Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-10-2017, 10:26 PM   #16
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012

Originally Posted by tony4260 View Post
I'm taking it that these two lines are equivalent:

echo "${@: (-2):2}" # prints the last two arguments.
echo "${ARGV[@]: (-2):2}"

And in those lines the -2 is saying "start 2 back from the last" (the last is -1, the one before that is -2 so:
Yes, pretty much, assuming you copied the argument list into ARGV in the way I posted. Bash treats the "$@" parameter essentially as an array, although there are minor differences based on historical contingency, particularly the 0 index needing to be explicitly expanded. If you just did ARGV=( "$@" ), then the index numbers would fail to match up. That wouldn't affect negative expansion, but it would affect positive expansions. See below.

./argumentstuff.bash this that those many
many would be -1, so the -2 is starting at those.

Then the 2 thats after the -2 is saying do that to two items. so if it was ${@: (-2):1} would only delete "those"?
Correct.In the range parameter substitution "${array[@]:idx:num}", the idx is the index of the first entry you want to output, and num is the number of items that you want from that point on. If the first number is negative, it counts backwards from the end of the array. The second number is optional if you want to output the entire range to the end. And as I pointed out above, arrays start from zero by default, except for $@, so ${array[@]:2:3} will expand to elements 3, 4, and 5, and "${@:2:3}" to elements 2, 3, and 4.

Edit: forget that last part. I was rushed for time earlier and got screwed up in my thinking. The two forms output the same, again assuming the array was set with a "$0" index.

Last edited by David the H.; 10-11-2017 at 05:12 AM. Reason: incorrect info
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-18-2017, 03:12 AM   #17
Senior Member
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,946

Rep: Reputation: Disabled
A portable solution.

usage='Some help...'

while [ "$#" -gt 0 ]; do 
  option="$1"; shift
  case "$option" in
    -- ) break ;;
    -b|--bar ) printf %s\\n 'bar' ;;
    -f|--foo ) printf %s\\n 'foo' ;;
    -h|--help ) printf %s\\n "$usage" ;;
    * ) printf %s\\n "Unrecognized option '$option', use -h for help."; exit 1 ;;


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
processing a ksh shell in bash environment ntoughe Programming 9 09-12-2005 05:09 AM
newbe bash question ( grep processing) therealbxp Programming 2 11-20-2004 07:40 AM
command line args in a countdown Squeak2704 Programming 1 04-26-2004 12:27 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM
>>> control mplayer with command args??? nibjb Linux - Software 0 07-16-2003 11:09 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 PM.

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