bash arguments
Hi,
I have a bash script where the last argument is a non-quoted path. THerefore, there can be a space in it. I would like to assign this path to a variable in my bash script but of course, if there's a space in there, the script considers it as a separate argument. Is there a way for me to say var2=(everything after the first argument passed to the bash shell) e.g. if the name of the bash shell is test.sh, when i run test.sh arg1 /opt/my software/run I would like for var2 to have the value /opt/my software/run Thanks... |
You'll have to 'escape' the space when you call the script.
E.g. test.sh arg1 /opt/my\ software/run Or, if the script should only ever have two arguments, you could join any arguments after the first inside the script. I think just escaping the space when you call the script is easier. If you use tab completion, bash will probably add the '\' for you. Dave |
ilikejam,
I'd like to join the arguments inside the script...but how do i do that?Thanks... |
Something along the lines of:
Code:
ARG2="" Dave |
You may want to play with example 9.6 on this webpage tutorial.
It explains the difference between "$*" and "$@". http://www.tldp.org/LDP/abs/html/int...es.html#APPREF The argument separation characters are defined by the $IFS variable which you can change in your script. Also the "$*" argument refers to the entire argument list as an entire word, starting with the command itself. Your best bet is to be consistent with all other commands and either quote the argument, or escape the space with a backslash when entering the argument. Also, put quotes around variables in scripts that expand into filenames. for example in this line for file in *.JPG; do mv "${file}" "${file%.JPG}.jpg"; done the quotes are necessary around ${file} if a file might possible contain a separation character. |
I stand corrected. I've been using $* wrong all this time. This could explain some strangeness in some stuff I was working on a while ago.
Cheers for the link jschiwal. Dave |
Quote:
But if you want to do it anyways: Code:
#!/bin/bash Code:
bash$ ./yourscript.sh arg1 /opt/my software/run |
All times are GMT -5. The time now is 09:10 PM. |