Skip to main content
Fixed quoting (would otherwise split the parameter values and perform filename globbing on the split values)
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

In addition (to Kusalananda's answer):

#! /bin/bash 

echo "(bash/ksh): ${@: -1}"
echo "(bash 3.x+): ${!#}"
echo "(bash 3.x+): $BASH_ARGV"
echo "(bash 3.x+/ksh): ${@:$#}"
echo "(bash 3.x+): ${BASH_ARGV[0]}"

and if you worry about portability:

#!/bin/bash

penultimate=''
ultimate=''

for p in $@"$@" ; do
    penultimate="$ultimate"
    ultimate="$p"
done

echo "penultimate=$penultimate"
echo "ultimate=$ultimate"

In addition (to Kusalananda's answer):

#! /bin/bash 

echo "(bash/ksh): ${@: -1}"
echo "(bash 3.x+): ${!#}"
echo "(bash 3.x+): $BASH_ARGV"
echo "(bash 3.x+/ksh): ${@:$#}"
echo "(bash 3.x+): ${BASH_ARGV[0]}"

and if you worry about portability:

#!/bin/bash

penultimate=''
ultimate=''

for p in $@ ; do
    penultimate="$ultimate"
    ultimate="$p"
done

echo "penultimate=$penultimate"
echo "ultimate=$ultimate"

In addition (to Kusalananda's answer):

#! /bin/bash 

echo "(bash/ksh): ${@: -1}"
echo "(bash 3.x+): ${!#}"
echo "(bash 3.x+): $BASH_ARGV"
echo "(bash 3.x+/ksh): ${@:$#}"
echo "(bash 3.x+): ${BASH_ARGV[0]}"

and if you worry about portability:

#!/bin/bash

penultimate=''
ultimate=''

for p in "$@" ; do
    penultimate="$ultimate"
    ultimate="$p"
done

echo "penultimate=$penultimate"
echo "ultimate=$ultimate"
explained the "in addition"
Source Link
Ljm Dullaart
  • 5.1k
  • 14
  • 27

In addition (to Kusalananda's answer):

#! /bin/bash 

echo "(bash/ksh): ${@: -1}"
echo "(bash 3.x+): ${!#}"
echo "(bash 3.x+): $BASH_ARGV"
echo "(bash 3.x+/ksh): ${@:$#}"
echo "(bash 3.x+): ${BASH_ARGV[0]}"

and if you worry about portability:

#!/bin/bash

penultimate=''
ultimate=''

for p in $@ ; do
    penultimate="$ultimate"
    ultimate="$p"
done

echo "penultimate=$penultimate"
echo "ultimate=$ultimate"

In addition:

#! /bin/bash 

echo "(bash/ksh): ${@: -1}"
echo "(bash 3.x+): ${!#}"
echo "(bash 3.x+): $BASH_ARGV"
echo "(bash 3.x+/ksh): ${@:$#}"
echo "(bash 3.x+): ${BASH_ARGV[0]}"

and if you worry about portability:

#!/bin/bash

penultimate=''
ultimate=''

for p in $@ ; do
    penultimate="$ultimate"
    ultimate="$p"
done

echo "penultimate=$penultimate"
echo "ultimate=$ultimate"

In addition (to Kusalananda's answer):

#! /bin/bash 

echo "(bash/ksh): ${@: -1}"
echo "(bash 3.x+): ${!#}"
echo "(bash 3.x+): $BASH_ARGV"
echo "(bash 3.x+/ksh): ${@:$#}"
echo "(bash 3.x+): ${BASH_ARGV[0]}"

and if you worry about portability:

#!/bin/bash

penultimate=''
ultimate=''

for p in $@ ; do
    penultimate="$ultimate"
    ultimate="$p"
done

echo "penultimate=$penultimate"
echo "ultimate=$ultimate"
Source Link
Ljm Dullaart
  • 5.1k
  • 14
  • 27

In addition:

#! /bin/bash 

echo "(bash/ksh): ${@: -1}"
echo "(bash 3.x+): ${!#}"
echo "(bash 3.x+): $BASH_ARGV"
echo "(bash 3.x+/ksh): ${@:$#}"
echo "(bash 3.x+): ${BASH_ARGV[0]}"

and if you worry about portability:

#!/bin/bash

penultimate=''
ultimate=''

for p in $@ ; do
    penultimate="$ultimate"
    ultimate="$p"
done

echo "penultimate=$penultimate"
echo "ultimate=$ultimate"