My question is how can I make this bash code run:
#!/bin/sh
actionString = printpage
curl 'www.example.com/index.php?action=$actionString;post=5'
My problem is that if I do not escape the URL with quotation marks, then it will stop processing the URL after the ";", however if I do have it in quotation marks it won't recognize the variable. Is there some trick to getting past this? Thanks for the help.
Use the quotes that don't inhibit parameter substitution.
curl "http://www.example.com/index.php?action=$actionString;post=5"
Variables are NOT evaluated within single quotes:
greeting=hello
echo 'say $greeting; ...' # outputs literally: say $greeting; ...
Variables ARE evaluated within double quotes:
echo "say $greeting" # outputs: say hello; ...
You can quote only the part of parameters that needs quoting, and you can use double quotes for some parts and single quotes for others. All of these are equivalent:
echo say $greeting"; ..."
echo say $greeting'; ...'
echo "say $greeting"'; ...'
echo say $greeting\; ...
So in your case, these would all work:
curl "http://www.example.com/index.php?action=$actionString;post=5"
curl http://www.example.com/index.php?action="$actionString;post=5"
curl http://www.example.com/index.php?action="$actionString;"post=5
and if actionString doesn't have special characters, this would work too:
curl http://www.example.com/index.php?action=$actionString';post=5'
Related
Sorry if the question is very straight forward but am a newbie to shell scripting.
I am trying to write something like this :
for i in {1..20}
do
curl "something $i ........ -d 'something "$i" something' "
done
The problem is that the second $i inside the single quotes part '' is not being replaced. What should be done to get it working ?
As said above, parameters are not expanded inside single quotes, you have to use double quotes. The only point is that since it occurs in a already double-quoted string, you have to escape them with a backslash (\), like this:
$ foo=bar
$ eval "echo \"something \\\"$foo\\\"\""
something "bar"
Note that there are three \ before the innermost ", as this will be expanded twice (once when evaluating the argument of eval and once when evaluating the argument of echo)
This is because variables inside the single quotes '' are not being replaced. If you want variable substitution, you need to get rid of the single quotes.
You coul maybe pre-initialize that variable
foo="something $i bla bla bla"
curl "something ... ${foo}something"
Your quoting is not correct. You don't need double quotes around the second $i because the whole thing is already surrounded in double-quotes.
Change it to the following:
for i in {1..20}
do
curl "something $i ........ -d 'something $i something'"
done
I'm trying to execute a make command from an text file using a bash script.
The make command has an parameter with space and it is not treating the parameter content as one. How do I escape double quote in this case?
$( echo "make -C ... INSTALL_MOD_STRIP="--param-one --param-two=.note.gnu.build-id" ARCH=arm64 ..." )
In this case the double quotes of INSTALL_MOD_STRIP are not considered after echo command and --param-two=.note.gnu.build-id is not considered part of such keyword.
How do I escape double quote to the content of INSTALL_MOD_STRIP= to be treated as one by make command?
I really can't understand what you're trying to do here. What's the purpose of writing $(echo foo)? Why don't you just write foo directly?
In any event, the reason double-quotes are disappearing from your expression:
echo "make -C ... INSTALL_MOD_STRIP="--param-one --param-two=.note.gnu.build-id" ARCH=arm64 ..."
is because quotes don't nest (how would the parser know to do that?) The above is parsed by the shell as the following words: echo, then the stuff between the first two quotes: make -C ... INSTALL_MOD_STRIP=, then the not-in-quotes words --param-one and --param-two=.note.gnu.build-id, then the stuff between the second two quotes: ARCH=arm64 ....
If you want to nest quotes you have to escape the inner quotes:
echo "make -C ... INSTALL_MOD_STRIP=\"--param-one --param-two=.note.gnu.build-id\" ARCH=arm64 ..."
Or even better, use single quotes as either the outer quotes (if the inner content doesn't have any variables) or the inner quotes (if the content does have variables):
echo "make -C ... INSTALL_MOD_STRIP='--param-one --param-two=.note.gnu.build-id' ARCH=arm64 ..."
I have a bash script that takes a url with variables and writes it to a file, problem is the ampersand is interfering and being interpreted as a command / control character.
In this situation the string cannot be escaped BEFORE being passed to the script and I have yet to find any way to do this.
if [ $1 ] ; then
url=$1
printf %q "$url" > "/somepath/somefile"
fi
with $1 being for example localhost?x=1&y=2&z=3
What get's printed is only the part before the first ampersand: "localhost?x=1"
I have also tried echo instead of printf but it's exactly the same ??
Your script is fine, but you need to invoke the script with a quoted parameter:
./myscript.sh "localhost?x=1&y=2&z=3"
There is no problem with echo nor print. The problem is that when you run the script, it starts those 2 jobs in background. For more information you can check: http://hacktux.com/bash/ampersand.
You can simply start script with 'localhost?x=1&y=2&z=3' in apostrophes, so bash will not treat ampersand as operator but just as normal character.
Quote things. Replace all $1s with "$1"s. And quote argument when you actually invoke your script.
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.