ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$# 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).
#!/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.
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172
Original Poster
Rep:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.