I have a shell variable in a for loop and I would like to indicate the end of the name of this variable:
$(DEBUG)for extblock in $(EXT_BLOCKS_LIST); \
do cat syn/ext_$$extblock_syn.tcl >> syn/$(SYN_TCL_SCRIPT); \
done;
My variable is only $$extblock but make takes $$extblock_syn as variable.
I don't achieve to put () correctly. Is there another character to indicate the end of a variable name?
Try this:
do cat syn/ext_$${extblock}_syn.tcl >> syn/$(SYN_TCL_SCRIPT);\
Braces can be used as delimiters. Also see the bash manual on shell parameter expansion.
Related
Example:
testing="test"
var="\"${testing} the variable\""
testing="Update"
echo "$var"
Output:
test the variable
Required output:
Update the variable
Variables are expanded when the string is used, they're not templates that remember the variable substitution.
If you need to do this, you need to put the variable literally into the string, and use eval.
testing="test"
var='"${testing} the variable"'
testing="Update"
eval "echo $var"
But eval is dangerous -- it will execute any shell commands in the string. A better solution would probably be to use some kind of placeholder string and replace it using the shell expansion operator.
var='#testing# the variable'
testing="Update"
echo "${var//#testing#/$testing}"
I have bash script which calls another script(some_script). some_script expects some input from the user. I have used printf statement for this purpose.
But the problem is the variable value is not being accepted by the target script. I think this is because '\' is being taken as an escape character in the script
The statement somewhat looks like this
printf 'yes\n$var1\n$var2\n$var3' | some_script
If i directly replace the variable with values it runs perfectly but i want the script to take the values from the variables. How do i achieve this?
There is a difference between " and '. Try
printf "yes\n$var1\n$var2\n$var3" | some_script
because with ' the variables won't get substituted.
Yes, \ is a character that has to be escaped.
Use \\n.
For more details we would need more details on how your script works.
Fairly simple, I have an echo statement in my shell (bash) script that I want to redirect to an external file as a means of logging. The script is run by the root user's crontab every hour (via sudo crontab -e).
I want this log file to reside in the same directory as the script.
My first attempt was,
echo "$(date) | blah blah" >> "$(pwd)/script.log"
However, this clearly does not work as the working directory of the root crontab (/root/) is not the same as the directory of the script. So following some advice on SO I did instead,
script_dir=$(dirname $0)
echo "$(date) | blah blah" >> "$(script_dir)/script.log"
This time, for reasons I do not yet understand, the log file is saved under /, as in /script.log.
Logically one would assume that the variable script_dir was evaluated to an empty string and so "$(script_dir)/script.log" was evaluated as "/script.log".
But as a test, I wrote a simple test script,
echo "$(dirname $0)"
and ran it from /. Sure enough, I get a proper non-empty output: /home/pi/scripts/testscripts/dirname.sh (where the test script I wrote resides).
So what's the deal, and how do I get this to work?
p.s. bash --version says I am currently running GNU bash, version 4.3.30(1)-release
You need the curly braces within double-quotes to expand variables in bash, something like,
echo "$(date) | blah blah" >> "${script_dir}/script.log"
Shell Parameter Expansion
The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion.
The basic form of parameter expansion is ${parameter}. The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character that is not to be interpreted as part of its name.
More on Parameter expansion
${PARAMETER}
The easiest form is to just use a parameter's name within braces. This is identical to using $FOO like you see it everywhere, but has the advantage that it can be immediately followed by characters that would be interpreted as part of the parameter name otherwise. Compare these two expressions (WORD="car" for example), where we want to print a word with a trailing "s":
echo "The plural of $WORD is most likely $WORDs"
echo "The plural of $WORD is most likely ${WORD}s"
Why does the first one fail? It prints nothing, because a parameter (variable) named "WORDs" is undefined and thus printed as "" (nothing). Without using braces for parameter expansion, Bash will interpret the sequence of all valid characters from the introducing "$" up to the last valid character as name of the parameter. When using braces you just force Bash to only interpret the name inside your braces.
This line has an error :
echo "$(date) | blah blah" >> "$(script_dir)/script.log"
It should be :
echo "$(date) | blah blah" >> "$script_dir/script.log"
The "$(script_dir)" syntax tries to execute a command named script_dir and capture its output to use as a value inside the string. What you need is a simple variable expansion, $script_dir, which simply extracts the value of a variable named script_dir.
I have a script I am trying to call that needs to have the $ symbol passed to it. If I run the script as
./script "blah$blah"
it is passed in fine but then the script calls another program I have no control over which then expands the parameter to just "blah". The program is being called by the command program $#. I was wondering if there was a way to prevent the parameter from being expanded when passed to the next script.
Escape the character $ with: \, e.g.: "This will not expand \$hello"
use single quotes: 'This will not expand $hello'
Use a HERE DOC:
<<'EOF'
This will not expand $hello
EOF
In your case I recommend using single quotes for readability: ./script 'blah$blah'.
A couple of options involving changing the quoting:
./script 'blah$blah'
./script "blah\$blah"
I hope this helps.
Call using single quotes:
./script 'blah$blah'
Or escape the $
./script "blah\$blah"
If I have a variable containing an unescaped dollar sign, is there any way I can echo the entire contents of the variable?
For example something calls a script:
./script.sh "test1$test2"
and then if I want to use the parameter it gets "truncated" like so:
echo ${1}
test1
Of course single-quoting the varaible name doesn't help. I can't figure out how to quote it so that I can at least escape the dollar sign myself once the script recieves the parameter.
The problem is that script receives "test1" in the first place and it cannot possibly know that there was a reference to an empty (undeclared) variable. You have to escape the $ before passing it to the script, like this:
./script.sh "test1\$test2"
Or use single quotes ' like this:
./script.sh 'test1$test2'
In which case bash will not expand variables from that parameter string.
The variable is replaced before the script is run.
./script.sh 'test1$test2'
by using single quotes , meta characters like $ will retain its literal value. If double quotes are used, variable names will get interpolated.
As Ignacio told you, the variable is replaced, so your scripts gets ./script.sh test1 as values for $0 and $1.
But even in the case you had used literal quotes to pass the argument, you shoudl always quote "$1" in your echo "${1}". This is a good practice.