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.
Related
What do those two assignations (i and C omitting the first one to void) do? Is it some kind of regex for the variable? I tried with bash, but so far there were no changes in the output of my strings after instantiating them with "${i//\\/\\\\}" or "\"${i//\"/\\\"}\""
C=''
for i in "$#"; do
i="${i//\\/\\\\}"
C="$C \"${i//\"/\\\"}\""
done
${i//\\/\\\\} is a slightly complicated-looking parameter expansion:
It expands the variable $i in the following way:
${i//find/replace} means replace all instances of "find" with "replace". In this case, the thing to find is \, which itself needs escaping with another \.
The replacement is two \, which each need escaping.
For example:
$ i='a\b\c'
$ echo "${i//\\/\\\\}"
a\\b\\c
The next line performs another parameter expansion:
find " (which needs to be escaped, since it is inside a double-quoted string)
replace with \" (both the double quote and the backslash need to be escaped).
It looks like the intention of the loop is to build a string C, attempting to safely quote/escape the arguments passed to the script. This type of approach is generally error-prone, and it would probably be better to work with the input array directly. For example, the arguments passed to the script can be safely passed to another command like:
cmd "$#" # does "the right thing" (quotes each argument correctly)
if you really need to escape the backslashes, you can do that too:
cmd "${#//\\/\\\\}" # replaces all \ with \\ in each argument
It's bash parameter expansions
it replace all backslashes by double backslashes :"${i//\\/\\\\}
it replace all \" by \\" : ${i//\"/\\\"}
Check http://wiki.bash-hackers.org/syntax/pe
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.
I need some help with the following simple bash script, where the variable i does not seem to get substituted when running curl (causing an error).
(This is just a simple abstraction of the actual script)
for i in {1..3}
do
HTML=$(curl -s 'http://example.com/index.php?id=$i')
done;
Variables are not substituted within single quotes. You have to use double quotes in this case:
for i in {1..3}; do
HTML=$( curl -s "http://example.com/index.php?id=$i" )
done
From http://tldp.org/LDP/abs/html/varsubn.html
Enclosing a referenced value in double quotes (" ... ") does not
interfere with variable substitution. This is called partial quoting,
sometimes referred to as "weak quoting." Using single quotes (' ... ')
causes the variable name to be used literally, and no substitution
will take place. This is full quoting, sometimes referred to as
'strong quoting.'
A
e.g ksh myshell.sh parm1, $$param2.
I want to assign the second parameter to a variable inside my shell script
e.g. var1=$2
and then use it as $var1,
i.e. var1 should have value $$param2 when I use it as $var1 inside my shell script.
That sounds about right. You just need to make sure you quote the call to my shell.sh correctly, so that the shell doesn't expand the $$ before your script is even called.
ksh myshell.sh parm1 '$$param2'
or
ksh myshell.sh parm1 $\$param2
Note that you don't need to escape both dollar signs, although you can if you like. It is not sufficient to escape only the first one, since $param2 would still be expanded.
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.