Created
October 10, 2014 02:25
-
-
Save kuy/70cb9842246a4e074f81 to your computer and use it in GitHub Desktop.
Swaps positional parameters in Bash script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "before: $#: $@" | |
pos=1 | |
for arg in "$@"; do | |
echo "$pos: $arg" | |
pos=$((pos + 1)) | |
done | |
args= | |
pos=0 | |
for arg in "$@"; do | |
rpos=$(($# - pos - 1)) | |
args[rpos]="$arg" | |
pos=$((pos + 1)) | |
done | |
echo "array: ${#args[@]}: ${args[@]}" | |
pos=1 | |
for arg in "${args[@]}"; do | |
echo "$pos: $arg" | |
pos=$((pos + 1)) | |
done | |
set -- "${args[@]}" | |
echo "after: $#: $@" | |
pos=1 | |
for arg in "$@"; do | |
echo "$pos: $arg" | |
pos=$((pos + 1)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment