I would like to set a variable in bash called test_var
Basically, I want echo test_var to output:
%let output="file_20120601.csv";
where 20120601 is a variable. I am trying to do this by using:
test_var='%let output="file_$1.csv";'
echo test_var
this doesn't work because $1 is not interpreted as variable, but interpreted as literally $1
Does anybody know how I can modify this to get it to do what I want it to do?
It doesn't work because of single quotes. They make everything literal.
$ var='123'
$ foo="\"%hi there file_$var\""
$ echo $foo
"%hi there file_123"
this works too
test_var='%let output="file_'"$1"'.csv";'
because variables are not expanded between single quotes.
and we can concatenate strings between single quotes and double quotes just writing strings beside.
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 a variable containing a string with a quote:
echo $variable
It's my variable
to be able to use this variable as a legend for ffmpeg, I need to add 5 backslashes before the quote:
variable="It\\\\\'s my variable"
I'm confused as to what syntax I should use, as the backslashes and quotes have very specific meanings in bash replace commands. I have tried this:
variable=`echo $variable | tr "'" "\\\\\'"`
but it does not produce the correct result
You can just use some single quotes yourself to tell bash not to interpret those slashes:
variable="It"'\\\\\'"'s my variable"
Edit: To convert an existing variable use:
variable=${variable//\'/'\\\\\'\'}
This works fine, is portable and does not have the problems of sed:
echo "$v" |perl -pe "s/'/\x5c\x5c\x5c\x5c\x5c'/g"
PS: \x5c is the ascii code of slash \
I'm still new at shell scripting
I want to assign * to a variable an print it. Write now I'm just printing it with:
echo -e "\052"
Is there a way to assign that value to a variable?
Use $(cmd) or `cmd` to capture a command's output. The $(...) form is preferred because it's easier to nest.
var=$(echo -e "\052")
The shell will interpret escape sequences inside $'...'. That's single quotes with a dollar sign in front.
var=$'\052'
Or of course you could write the asterisk directly. Quote it to prevent wildcard expansion.
var='*'
When you print it, make sure to quote the variable. It's annoying to always have to type double quotes any time you use a variable, but it's usually the right thing to do.
echo "$var" # yes
echo $var # no
Using backticks, ``, allows you to capture the output of a command. Many shells have a more sophisticated syntax, $(). But backticks are the most portable.
var=`echo -e "\052"`
What is the difference of echoing these variable in bash scripting?
EXAMPLE:
I declare a variable
VARIABLE="Hello World"
echo $VARIABLE
What's the difference between the one above and this below?
echo ${VARIABLE}
Does it make a difference if I put {} or not?
No difference in your code
The curly braces, To delimiting a variable name are used for parameter expansion so you can do things like
Truncate a variable' content
$ var="abcde"; echo ${var%e*}
abcd
Make substitutions similar to sed
$ var="abcde"; echo ${var/e/1}
abcd1
Braces can also be useful when the expansion occurs in certain contexts. For example:
FOO=bar
echo $FOO1 # tries to print the value of a variable named "FOO1"
echo ${FOO}1 # prints "bar1"
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.