Skip to content

Instantly share code, notes, and snippets.

@greymd
Last active May 1, 2017 05:15
Show Gist options
  • Save greymd/5b92714f80deeb9cb3d97523acba6c41 to your computer and use it in GitHub Desktop.
Save greymd/5b92714f80deeb9cb3d97523acba6c41 to your computer and use it in GitHub Desktop.
difference between "${1+$@}" and ${1+"$@"}
#!/bin/bash
set -u
argtest() {
printf '$@: '
echo "$@"
printf '$# '
echo $#
printf '$1 '
echo $1 ;shift
printf '$2 '
echo $1 ;shift
printf '$3 '
echo $1 ;shift
}
echo 'case1: ${1+$@}'
argtest "${1+$@}"
echo 'case2: ${1+"$@"}'
argtest ${1+"$@"}
# --------------------
# Result -> no difference?
# --------------------
# $ bash --version
# GNU bash, version 4.3.42(1)-release (x86_64-apple-darwin14.5.0)
# Copyright (C) 2013 Free Software Foundation, Inc.
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
#
# This is free software; you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
# $ ./setu.sh "a a" "b b b" "c"
# case1: ${1+$@}
# $@: a a b b b c
# $# 3
# $1 a a
# $2 b b b
# $3 c
# case2: ${1+"$@"}
# $@: a a b b b c
# $# 3
# $1 a a
# $2 b b b
# $3 c
# $ ./setu.sh
# case1: ${1+$@}
# $@:
# $# 1
# $1
# $2 ./setu.sh: line 12: $1: unbound variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment