Get Last Parameter on Shell Script

Getting the last argument passed to a shell script

This is a bit of a hack:

for last; do true; done
echo $last

This one is also pretty portable (again, should work with bash, ksh and sh) and it doesn't shift the arguments, which could be nice.

It uses the fact that for implicitly loops over the arguments if you don't tell it what to loop over, and the fact that for loop variables aren't scoped: they keep the last value they were set to.

How to get the last parameter in shell

If all holds the $@, then the last argument is ${all[*]: -1}

How to get the second last argument from shell script?

set -- "first argument" "second argument" \
"third argument" "fourth argument" \
"fifth argument"
second_to_last="${@:(-2):1}"
echo "$second_to_last"

Note the quoting, which ensures that arguments with whitespace stick together -- which your original solution doesn't do.

How to use arguments from previous command?

Just as M-. (meta-dot or esc-dot or alt-dot) is the readline function yank-last-arg, M-C-y (meta-control-y or esc-ctrl-y or ctrl-alt-y) is the readline function yank-nth-arg. Without specifying n, it yanks the first argument of the previous command.

To specify an argument, press Escape and a number or hold Alt and press a number. You can do Alt--to begin specifying a negative number then release Alt and press the digit (this will count from the end of the list of arguments.

Example:

Enter the following command

$ echo a b c d e f g
a b c d e f g

Now at the next prompt, type echo (with a following space), then

Press Alt-Ctrl-y and you'll now see:

$ echo a

without pressing Enter yet, do the following

Press Alt-3 Alt-Ctrl-y

Press Alt-- 2 Alt-Ctrl-y

Now you will see:

$ echo ace

By the way, you could have put the echo on the line by selecting argument 0:

Press Alt-0 Alt-Ctrl-y

Edit:

To answer the question you added to your original:

You can press Alt-0 then repeatedly press Alt-. to step through the previous commands (arg 0). Similarly Alt-- then repeating Alt-. would allow you to step through the previous next-to-last arguments.

If there is no appropriate argument on a particular line in history, the bell will be rung.

If there is a particular combination you use frequently, you can define a macro so one keystroke will perform it. This example will recall the second argument from previous commands by pressing Alt-Shift-Y. You could choose any available keystroke you prefer instead of this one. You can press it repeatedly to step through previous ones.

To try it out, enter the macro at a Bash prompt:

bind '"\eY": "\e2\e."'

To make it persistent, add this line to your ~/.inputrc file:

"\eY": "\e2\e."

Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers.

How to fetch last argument and stop before last arguments in shell script?

Using bash:

fname=${!#}
for a in "${@:1:$# - 1}"
do
echo "$a"
cat "$a" >>"$fname"
done

In bash, the last argument to a script is ${!#}. So, that is where we get the file name.

bash also allows selecting elements from an array. To start with a simple example, observe:

$ set -- a b c d e f
$ echo "${@}"
a b c d e f
$ echo "${@:2:4}"
b c d e

In our case, we want to select elements from the first to the second to last. The first is number 1. The last is number $#. We want to select all but the last. WE thus want $# - 1 elements of the array. Therefore, to select the arguments from the first to the second to last, we use:

${@:1:$# - 1}

Extract parameters before last parameter in $@

To remove the last item from the array you could use something like this:

#!/bin/bash

length=$(($#-1))
array=${@:1:$length}
echo $array

Even shorter way:

array=${@:1:$#-1}

But arays are a Bashism, try avoid using them :(.

How can I recall the argument of the previous bash command?

You can use $_ or !$ to recall the last argument of the previous command.

Also Alt + . can be used to recall the last argument of any of the previous commands.

Bash - how to get a last character of a positional Parameter?

Trivially

echo "${1: -1}"

The numbered arguments use exactly the same syntax as any other variables.

The parameter expansion ${str: -1} is a Bash extension; if you need POSIX sh compatibility, you need two steps.

_=${1%?}
echo "${1#"$_"}"

The first expansion returns the string with its last character removed; then, we remove this string from the prefix, yielding just the character we chopped off before.

get n latest arguments shell script

You can use a bash array and then slice it:

As @GordonDavisson pointed out, you can directly slice $@

#!/bin/bash

files=( "${@:7}" )

# show content of the array
declare -p files


Related Topics



Leave a reply



Submit