Passing a variable value to an option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a variable value to an option
# 1  
Old 09-28-2014
Passing a variable value to an option

Hello,

I am new to shell (i.e. linux bash) programming and have the following question:

When using this wget command I can download a certain website that needs login information by passing a previously acquired cookie:

Code:
wget --header='Cookie: SID=ac658ef0876b24ff456' somewebsite.com

As cookies sometimes get very long, I want to shorten the command by storing the cookie in a variable and passing the value of that variable as an option to wget like this:

Code:
cookie='Cookie: SID=ac658ef0876b24ff456'

wget --header=$cookie somewebsite.com

But this doesn't seem to work at all. The cookie is not being passed to the website as in the first case. Who knows what I am doing wrong?
# 2  
Old 09-28-2014
Spaces in an unquoted variable expansion will split it into several parts. Put it in double quotes to avoid this.

Code:
wget --header="$cookie" ...

The quotes will not appear in the string. You can convince yourself of this with printf:

Code:
printf "%s\n" "$cookie"

# 3  
Old 09-29-2014
iggy98,
Corona688 is right, but additionaly to that, the --header option of the wget command obviously requires the cookie to be in single quotes.

Try
Code:
cookie="'Cookie: SID=ac658ef0876b24ff456'"
wget --header=$cookie somewebsite.com

or
Code:
cookie='Cookie: SID=ac658ef0876b24ff456'
wget --header="'$cookie'" somewebsite.com

# 4  
Old 09-29-2014
Quote:
Originally Posted by junior-helper
iggy98,
Corona688 is right, but additionaly to that, the --header option of the wget command obviously requires the cookie to be in single quotes.
It does not.

Quotes do not work that way.

Play with single-quotes and printf until you've satisfied yourself of the fact that the shell does not pass literal single or double quote characters into a program. Then realize how nesting single quotes in double quotes accomplishes nothing but injecting weird characters where they do not belong.

Last edited by Corona688; 09-29-2014 at 12:45 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-29-2014
Indeed, I tried junior-helper's examples and it didn't work either. Corona688 you got it right. I was already in despair about that issue - thank you very much !
This User Gave Thanks to iggy98 For This Post:
# 6  
Old 09-29-2014
Glad to help, is the issue solved then?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing positioning array as whiptail -- menu option

I may have asked this before, so forgive OF. Problem: I can pass positioning array as -- menu option to whiptail, but it does not show in the whiptail form as an array - only single (first member "lsusb" ) entry / line shows up. Code: DynamicEntry=$(whiptail \ --title "DEBUG... (1 Reply)
Discussion started by: annacreek
1 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

4. Shell Programming and Scripting

Passing parameter and option together

Hi , How can I pass option and parameter together? I want like script.sh par1 par2 -s option1 And when I am doing echo $1 echo $2 #opt is the variable where we are storing the option value through getops echo $opt output should be (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

5. Shell Programming and Scripting

passing an option as an argument!

Hi Folks I have got to the point where I can specify the arguments but how to pass an option is still mystery to me. Example: temp.csh a b c d set temp1 = $argv set temp2 = $argv set temp3 = $argv echo $temp1 a echo $temp2 b echo $temp3 c d I WANT: temp.csh a b c d -S 1 set temp1... (2 Replies)
Discussion started by: dixits
2 Replies

6. Shell Programming and Scripting

How to use a variable as a command option?

I am just learning shell scripting and already I found out I have the bad habit of thinking that it is similar to php or c. I learned some basics and now encountered this problem: On shell it is possible to type: $ date --date="2009-10-10 09:08:34" Sat Oct 10 09:08:34 CEST 2009 ... (2 Replies)
Discussion started by: quinestor
2 Replies

7. Shell Programming and Scripting

variable option not properly recognized

I'm creating a junk script which utilizes either -l or -p to list files or remove files, respectively, in the junk directory. When I run this code, inputting '-p' is unrecognized through the whole if/else block and falls to the last else (echo do nothing). In addition, I switched tests and tested... (2 Replies)
Discussion started by: haishuva
2 Replies

8. Shell Programming and Scripting

passing a variable inside another variable.

Any help would be great. I know this is a dumb way of doing this, but I would like to know if there is a solution doing it this way. I'm very new at this and I'd like to learn more. Thanks! :D:D count=0 while ; do echo "enter your name" read name_$count let count=count+1 done ... (2 Replies)
Discussion started by: reconflux
2 Replies

9. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

10. Shell Programming and Scripting

Env Variable substituion in Sed (-s option)

Folks, I've been trying to use the ENV variable with slashes(/) in its value inside the sed substitution.. Sed 's/myval/'$MYVAL'/' file1 >> file.tmp If MYVAL=<sometext>, it works. if MYVAL=/home/venkat, it doesnt. *************************** bash-2.05$ export VAL=/home/venkat... (5 Replies)
Discussion started by: gvsreddy_539
5 Replies
Login or Register to Ask a Question