Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

shift - shift positional parameters

&pagelevel(4)&pagelevel

The POSIX shell built-in shift shifts the values of positional parameters over to the left. shift without arguments thus results in the following:

  • Shell parameter $0 (the command name) is unaffected.

  • The original value of $1 is lost; this value can no longer be accessed.

  • Instead, $1 takes the original value of $2, $2 that of $3, and so on until $8, which takes the value of $9.

  • The tenth command-line argument is passed to the last positional parameter $9.

  • $# is reduced by one.

  • $* and $@ contain all command-line arguments, starting with the new value of $1. The original value of $1 is dropped.

By default, the shell provides positional parameters which give you direct access to only the first nine command-line arguments of a set command or a shell script. This restriction can be bypassed with shift, since it allows you to move values over to the left by the required number of places.


Syntax


shift [ n]

n

A positive integer; shift is executed n times. This means that positional parameter $1 takes the value of the (n+1)th command line argument, etc.

If the value of n is too large, shift issues an error message as soon as no command-line argument is available for $1; i.e. $# is equal to 0.

n not specified:
shift is executed once.

If $1 already contains the last argument, the next invocation of shift will result in an error message.

Error

sh: shift: bad number

This error message is issued if number >$#. As a result no value could be assigned to positional parameter $1.

Locale

The following environment variables affect the execution of shift:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

The tenth command-line argument is to be accessed in a shell script:

:
arg1=$1
shift
arg10=$9
:

The original value of $1 is assigned to the arg1 variable. The value thus remains accessible even after the shift command.

Example 2

The following interactive session demonstrates the changes that take place in the values of the shell parameters $1, $*, and $# when shift is invoked:

$ set 1 2 3 4 5 6 7 8 9 10 11 12
$ echo $1
1
$ echo $#
12
$ echo $*
1 2 3 4 5 6 7 8 9 10 11 12
$ shift 6
$ echo $1
7
$ echo $*
7 8 9 10 11 12
$ echo $#
6

See also

set