How to use environment variable inside quote string in Bash

I tried the following forms in a bash script form:

#!/bin/bash svn diff $@ --diff-cmd /usr/bin/diff -x "-y -w -p -W $COLUMNS" 

But I can not get the syntax to properly expand the COLUMNS environment COLUMNS .

I tried various forms:

 svn diff $@ --diff-cmd /usr/bin/diff -x '-y -w -p -W $COLUMNS' 

and

 svn diff $@ --diff-cmd /usr/bin/diff -x '-y -w -p -W ${COLUMNS}' 

and

 eval svn diff $@ --diff-cmd /usr/bin/diff -x "-y -w -p -W $COLUMNS" 

Suggestions?

+48
bash quotes environment
May 08 '09 at 15:57
source share
5 answers

If you are not sure, you can use the "cols" request on the terminal and forget COLUMNS:

 COLS=$(tput cols) 
+12
May 09 '09 at 19:45
source share
β€” -

Just a short note / resume for everyone who came here through Google, looking for the answer to the general question asked in the title (like me). Any of the following should work to gain access to shell variables inside quotes:

 echo "$VARIABLE" echo "${VARIABLE}" 

Using single quotes is a major issue. According to the Bash Reference Guide :

Attachment of characters in single quotation marks ( ' ) stores the literal value of each character in quotation marks. A single quote may not appear between single quotes, even if it is preceded by a backslash. [...] Enclosing characters in double quotation marks ( " ) keep the literal value of all characters in quotation marks, except for $ , ` , \ , and when the history extension is enabled, ! . The characters $ and `retain their special meaning in double quotation marks ( see the "Shell Extensions" section.) A backslash retains its special meaning only when one of the following characters follows: $ , ` , " , \ or a new line. Double quotation marks remove backslash traces followed by one of these characters. Backslashes preceding characters without special meaning remain unchanged. A double quotation mark can be enclosed in double quotation marks, preceding it with a backslash. If enabled, story expansion will be performed, if only ! appearing in double quotes will not be executed using the backslash. Backslash preceding ! is not deleted. The special parameters * and @ are of particular importance when enclosed in double quotation marks (see Extending shell parameters).

In the specific case asked in the question, $ COLUMNS is a special variable that has non-standard properties (see lhunath answer above).

+246
Feb 23 2018-12-23T00:
source share

Please note that COLUMNS :

  • NOT an environment variable. This is a common bash parameter that is set by bash itself.
  • Set automatically after receiving a SIGWINCH signal.

This second point usually means that your COLUMNS will only be set in your interactive shell, not in a bash script.

If your script stdin connected to your terminal, you can manually view the width of your terminal by querying your terminal:

 tput cols 

And use this in your SVN command:

 svn diff "$@" --diff-cmd /usr/bin/diff -x "-y -w -p -W $(tput cols)" 

(Note: you should quote "$@" and stay away from eval )

+12
May 11 '09 at 7:30 a.m.
source share

The following script works for me for a few $COLUMNS values. I wonder if you are setting COLUMNS before this call?

 #!/bin/bash COLUMNS=30 svn diff $@ --diff-cmd /usr/bin/diff -x "-y -w -p -W $COLUMNS" 

Can you repeat $COLUMNS inside your script to make sure it is installed correctly?

0
May 08 '09 at 16:29
source share

You are doing it right, so I assume something else is to blame (and not export COLUMNS?).

The trick to debugging these cases is to create a specialized team (closing for guys in a programming language). Create a shell script called diff-columns:

 exec /usr/bin/diff -x -y -w -p -W "$COLUMNS" "$@" 

and just use

 svn diff "$@" --diff-cmd diff-columns 

Thus, your code is cleaner to read and more modular (top-down approach), and you can test diff-columns code separately separately (bottom-up approach).

0
May 09 '09 at 8:04 a.m.
source share



All Articles