LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-12-2004, 07:42 PM   #1
stuckinhell
LQ Newbie
 
Registered: Aug 2004
Posts: 3

Rep: Reputation: 0
Angry 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...
 
Old 08-12-2004, 08:45 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
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
 
Old 08-12-2004, 09:25 PM   #3
stuckinhell
LQ Newbie
 
Registered: Aug 2004
Posts: 3

Original Poster
Rep: Reputation: 0
ilikejam,
I'd like to join the arguments inside the script...but how do i do that?Thanks...
 
Old 08-12-2004, 10:00 PM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Something along the lines of:

Code:
ARG2=""

for go in $*
do
    if [ $go != $1 ]
        then
        ARG2="$ARG2 $go"
    fi
done
That will give you the second argument in $ARG2

Dave
 
Old 08-12-2004, 10:27 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 08-12-2004, 10:56 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
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
 
Old 08-13-2004, 05:10 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by stuckinhell
I'd like to join the arguments inside the script...but how do i do that?Thanks...
It had already been said, but also IMO it's really better to just pass the path parameter quoted, and if not that, at least have the space(s) escaped with a backslash.

But if you want to do it anyways:
Code:
#!/bin/bash

ARG1=$1
shift
ARGPATH="$@"

echo "First argument is: $ARG1"
echo "The path argument is: $ARGPATH"
Note: this will fail when there are more than one spaces in a row, for example:
Code:
bash$ ./yourscript.sh arg1 /opt/my               software/run
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
logname: no login name, -bash: [: too many arguments da_kidd_er Linux - General 1 10-27-2004 02:09 PM
[bash-scripting]functions + arguments hylke Programming 14 10-05-2004 01:48 AM
bash scripting - referring to external arguments into loops linsson Linux - General 2 07-23-2004 12:24 PM
BASH says "too many arguments" in terminal tmitch70377 Linux - Newbie 4 12-06-2003 05:19 PM


All times are GMT -5. The time now is 11:31 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration