This question already has answers here:
How to pass a variable in a curl command in shell scripting
(5 answers)
Closed 1 year ago.
My script deals with browser automation using cURL. I need to POST user input username. I can do this by hard-coding the username like this:
# sid -> session Id
# eid -> element Id (input box here)
curl -d '{"value":["username"]}' http://localhost:9515/session/$sid/element/$eid/value
This successfully posts username but I want to read username from user and pass this to value.
I tried:
read userName
curl -d '{"value":[$userName]}' http://localhost:9515/session/$sid/element/$eid/value
This gives me "missing command parameter" error. Passing only $userName instead of [$userName] gives "invalid argument: 'value' must be a list"
How do I pass a variable (userName) to the curl POST request in this case?
It looks like you've tried to use the variable within single quotes. Try to use double quotes instead:
read userName
curl -d "{\"value\":\"${userName}\"}" http://localhost:9515/session/$sid/element/$eid/value
Explanation:
Single quotes (' ') operate similarly to double quotes, but do not permit referencing variables, since the special meaning of $ is turned off. Within single quotes, every special character except ' gets interpreted literally. Consider single quotes ("full quoting") to be a stricter method of quoting than double quotes ("partial quoting").
Taken from: https://tldp.org/LDP/abs/html/quotingvar.html
Related
This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Closed 3 years ago.
I am trying read input to a variable then use this variable in a REST API call data field, but I am unable to escape the variable properly inside quotes.
I have tried backslash before the single quotes, before the double quotes and before both single and double quotes at the same time.
token=$(curl $VAULT_ADDR/v1/auth/ldap/login/$username -d '{"password": "$password"}' -k | jq '.auth.client_token')
echo token
As of now it reads $password as a string. Can't authenticate because the password is wrong then token equals null. I am trying to use the value of variable $password inside double quotes since the data field is supposed to be in JSON-format.
password=hunter2
'{"password": "$password"}' becomes
'{"password": "hunter2"}'
If I hardcode my password it succeeds
'{"password": "'$password'"}'
?
I am trying to write a script to change a password on multiple servers. The issue is that the password has special characters. This is the example of the echo command I am using:
echo -e "'P#7g$dkW$8Ej$5$%'\n'P#7g$dkW$8Ej$5$%'"
Here is the response:
[root#myserver ~]# echo -e "'P#7g$dkW$8Ej$5$%'\n'P#7g$dkW$8Ej$5$%'"
'P#7gEj$%'
'P#7gEj$%'
As you can see it is dropping a lot of characters. Any advice would be helpful.
Take out the double quotes and just use single quotes so variables (preceded by $) aren't expanded.
The reason why your string is interpreted that way is because you have used double-quotes to create the string. The single quote that you have set after that will be interpreted as a part of the string and has no special meaning anymore and doesn't make your password a literal string. That means that the password string is still in the scope of "" and $xyz will be interpreted as the xyz variable.
You can try to concatenate them like this
passwd1='password1'
passwd2='password2'
echo -e "$passwd1\n$passwd2"
or just leave out the double quotes in your solution.
Hi I have to call curl POST method for a number of records, using bash script. For that i am using while loop.
Actual posting with hardcoded values:
curl -X POST -u user:pass --data '<automation><operation action="assemble" package="service Item"/></automation>' http://localhost:8080/form
When i try to use while loop for a set of different actions and different packages, the $packagename field is not working. When i use $packagename as a variable to package option of data section, it is not taking, since we must pass data in double quotes. (" ") It is always taking the same value which is in double quotes.
Could someone suggest a solution for this?
End the single quoted string and switch to double quotes around the variable.
curl -X POST -u user:pass --data '<automation><operation action="assemble" package="'"$packagename"'"/></automation>' http://localhost:8080/form
I am working on a shell script to retrieve variable content from a JSON file via JQ. The JSON file is in string format (no matter whether this is a real string or a number) and to retrieve the variable in my bash script I did something like this
my_domain=$(cat /vagrant/data_bags/config.json | jq ."app"[0]."domain")
The above code once echoed results in "mydomain" with a beginning and a trailing quote sign. I though this was a normal behaviour of the echo command. However, while concatenating my variable with another shell command the system raise an error. For instance, the following command
cp /vagrant/public_html/index.php "/var/www/"+$my_domain+"/index.php"
fails with the following error
cp: cannot create regular file `/var/www/+"mydomain"+/index.php': No such file or directory
At this stage, I wasn't able to identify whether it's me doing the wrong concatenation with the plus sign or the variable is effectively including the quotes that in any case will end up generating an error.
I have tried to replace the quotes in my variable, but I ended up getting the system raising a "Command not found" error.
Can somebody suggest what am I doing wrong?
+ is not used for string concatenation in bash (or perl, or php). Just:
cp /vagrant/public_html/index.php "/var/www/$my_domain/index.php"
Embedding a variable inside a double-quoted text string is known as interpolation, and is one of the reasons why we need the $ prefix, to indicate that this is a variable. Interpolation is specifically not done inside single quoted strings.
Braces ${my_domain} are not required because the / directory separators are not valid characters in a variable name, so there is no ambiguity.
For example:
var='thing'
echo "Give me your ${var}s" # Correct, appends an 's' after 'thing'
echo "Give me your $vars" # incorrect, looks for a variable called vars.
If a variable (like 'vars') does not exist then (by default) it will not complain, it will just give an empty string. Braces (graph brackets) are required more in c-shell (csh or tcsh) because of additional syntax for modifying variables, which involves special trailing characters.
You don't need to use + to concatenate string in bash, change your command to
cp /vagrant/public_html/index.php "/var/www/"${my_domain}"/index.php"
My problem was not related only to the wrong concatenation, but also to the JQ library that after parsing the value from the JSon file was returning text between quotes.
In order to avoid JQ doing this, just add the -rawoutput parameter when calling JQ.
How do I write a shell script (bash on HPUX) that receives a string as an argument containing an asterisk?
e.g. myscript my_db_name "SELECT * FROM table;"
The asterisk gets expanded to all the file names in the current directory, also if I assign a variable like this.
DB_QUERY="$2"
echo $DB_QUERY
The asterisk "*" is not the only character you have to watch out for, there's lots of other shell meta-charaters that can cause problems, like < > $ | ; &
The simple answer is always to put your arguments in quotes (that's the double-quote, " ) when you don't know what they might contain.
For your example, you should write:
DB_QUERY="$2"
echo "$DB_QUERY"
It starts getting awkward when you want your argument to be used as multiple parameters or you start using eval, but you can ask about that separately.
You always need to put double quotes around a variable reference if you want to prevent it from triggering filename expansion. So, in your example, use:
DB_QUERY="$2"
echo "$DB_QUERY"
In the first example, use single quotes:
myscript my_db_name 'SELECT * FROM table;'
In the second example, use double quotes:
echo "$DB_QUERY"