LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-30-2006, 06:19 PM   #1
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
Finding the last command line argument (bash)


Is there a simple way to do it? Any help is appreciated.
 
Old 09-30-2006, 06:25 PM   #2
comprookie2000
Gentoo Developer
 
Registered: Feb 2004
Location: Fort Lauderdale FL.
Distribution: Gentoo
Posts: 3,291
Blog Entries: 5

Rep: Reputation: 54
I press the up arrow key. Is that what you mean?
 
Old 09-30-2006, 06:31 PM   #3
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Original Poster
Rep: Reputation: 30
I'm sorry, I meant inside a script.
 
Old 09-30-2006, 06:37 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

$# gives you the amount of arguments. I.e:
$ ./mysript a b c
3

$* gives you all the args in a string. I.e:
$ ./myscript a b c
a b c

See bash manpage for details.

Both or the last could be of use.

You can also take a look at shift, also a bash internal.

Don't know if you are writing/wrote this script, but if arguments become important and you need it to be more flexible also take a look at getopts (bash internal, see manpage/bash book).

Hope this helps.
 
Old 09-30-2006, 06:42 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Code:
#!/bin/bash

echo ${BASH_ARGV[0]}
Code:
$ ./test_script 1 2 3 test
test
$ ./test_script 1 2 3 test foobar
foobar

Last edited by Dark_Helmet; 09-30-2006 at 06:46 PM.
 
Old 09-30-2006, 06:44 PM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi, pete1234.

With some features found in the shell, you can.

Consider:
Code:
#!/bin/sh

# @(#) s1       Demonstrate how to obtain last positional parameter.

set -- 1 2 3 4four 5 6 7 8 9 10 11 12 13 14 15x

echo "All parameters:"
echo ":$*:"

echo
echo " Show that simple dereferencing does not work above 9."
echo $4
echo $15

echo
LAST=$#
echo " Number of last parameter is :$LAST:"

echo " Save all parameters."
SAVE="$@"

echo
echo " Shifting all parameters."
shift $(( $LAST-1 ))

echo
echo ' Now $1 is the old $15'
echo $1

echo
echo 'All parameters still in SAVE, and can be re-established with "set -- $SAVE"'
echo ":$SAVE:"
When run, this produces:
Code:
% ./s1
All parameters:
:1 2 3 4four 5 6 7 8 9 10 11 12 13 14 15x:

 Show that simple dereferencing does not work above 9.
4four
15

 Number of last parameter is :15:
 Save all parameters.

 Shifting all parameters.

 Now $1 is the old $15
15x

All parameters still in SAVE, and can be re-established with "set -- $SAVE"
:1 2 3 4four 5 6 7 8 9 10 11 12 13 14 15x:
The 4 and 15 have extra characters to make sure we're doing the right thing. Note that "$*" and "$@" are special to the shell. Use whichever one in appropriate. See man bash for details.

Best wishes ... cheers, makyo
 
Old 09-30-2006, 06:46 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

I like Dark_Helmet's solution, although I could not get that to work in bash version 2.05b.0(1) ... cheers, makyo

Last edited by makyo; 09-30-2006 at 06:51 PM.
 
Old 09-30-2006, 06:48 PM   #8
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Original Poster
Rep: Reputation: 30
I've tried using shift it doesn't seem to help. All arguments manipulate the last command line argument which is a file, so as I shift it screws things up(maybe I just don't know how to use shift properly?). I've also tried several variations of
Code:
$$#
to no avail. The closest I've come is using something like
Code:
echo $* | egrep -o 'regex'
. That normally works, but it seems like there's a better way.

#EDIT I guess there was no need for this rant. Thanks for the help.

Last edited by pete1234; 09-30-2006 at 06:50 PM.
 
Old 09-30-2006, 06:52 PM   #9
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Original Poster
Rep: Reputation: 30
Dark_Helmet could you explain your solution?
 
Old 09-30-2006, 06:53 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

You should use Dark_Helmet's method, it's the best one. I though using ARGV was only possible in perl, my bad.

Still, here's how you use $#, $* and shift in a nutshell:
Code:
#!/bin/bash
numArgs="$#"
echo $numArgs
echo $*
shift 2
echo $*
Sample run:
Code:
$ ./script a b c
3
a b c
c
 
Old 09-30-2006, 07:02 PM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
BASH_ARGV is a special built-in variable. It's an array of the command line arguments. More specifically, the man page says it's the arguments located on the stack. So it's safe to read them, but I strongly suggest not modifying them.

To access the array, use [X] to refer to a specific argument. The arguments are put on the stack "backwards" and explains why the last argument is indexed with 0.

Lastly, BASH_ARGV will change inside a function call (because the stack changes). So be careful where/how you access the variable.
 
Old 09-30-2006, 07:09 PM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
@Dark_Helmet:

Which bash/bash version are you using?

BASH_ARGV isn't part of GNU bash, version 2.05b.0(1)-release (i686-pc-linux-gnu), like makyo I can't get it to work with this release.
 
Old 09-30-2006, 07:11 PM   #13
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Original Poster
Rep: Reputation: 30
Works for me
Code:
bash --version
GNU bash, version 3.1.0(1)-release (i686-pc-linux-gnu)
Thanks everyone for the help.
 
Old 09-30-2006, 07:26 PM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Time for an upgrade
 
Old 09-30-2006, 07:41 PM   #15
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Sorry for the late reply... Looks like it might have been a feature for version 3.
Code:
$ bash --version
GNU bash, version 3.00.16(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
This is on my FC4 system.
 
  


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 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM
bash shell command line expansion hansi umayangan Linux - General 2 03-13-2005 11:31 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM
Finding out my video card from the command line devinWhalen Linux - Hardware 3 04-15-2004 11:59 AM


All times are GMT -5. The time now is 08:56 PM.

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