Windows
Linux
Terminal & Shell Scripting
Terminal
Windows shift equivalent in Linux
Shifts positional parameters ($1, $2...) left by one. Used to process command-line arguments in scripts.
Windows
→
shift
Linux equivalents
shift
shift
shift
Overview
What this command does
Shifts positional parameters ($1, $2...) left by one. Used to process command-line arguments in scripts.
Migration tip: Script arguments are
$1 $2 $3 etc. $@ is all arguments, $# is the count. shift removes $1 and shifts the rest down.Practical tasks
Common use cases
Shift script argument positions
Shifts positional parameters ($1, $2...) left by one. Used to process command-line arguments in scripts.
$1 $2 $3
Positional arguments
$@
All arguments as separate words
$#
Number of arguments
Ready to run
Copyable Linux commands
Review paths, device names, package names and permissions before running any command.
echo "All args: $@"
echo "Count: $#"
echo "First: $1"
while [ $# -gt 0 ]; do
echo "Arg: $1"
shift
done
Walkthrough
Examples with explanations
#!/bin/bash
# Script: myscript.sh arg1 arg2 arg3
echo "All args: $@"
echo "Count: $#"
echo "First: $1"
# Process all args one by one:
while [ $# -gt 0 ]; do
echo "Arg: $1"
shift
done
Reference
Common options and variations
| Command or option | Use |
|---|---|
$1 $2 $3 | Positional arguments |
$@ | All arguments as separate words |
$# | Number of arguments |
shift | Remove $1 and shift remaining down |
shift 2 | Shift by 2 positions |
Distro differences
Debian/Ubuntu vs Fedora vs Arch
This command is the same on Debian/Ubuntu, Fedora and Arch Linux when the relevant tool is installed.
| Distribution | Package manager / base | Equivalent command | Difference to notice |
|---|---|---|---|
| Debian/Ubuntu | same command | shift |
Same command on this distribution. |
| Fedora | same command | shift |
Same command on this distribution. |
| Arch | same command | shift |
Same command on this distribution. |
Keep learning