shell - Comment concrétiser les variables de chaîne dans Bash

Mots clés : bashshellsyntaxconcatstring-concatenationbash

meilleur 5 Réponses shell - Comment concrétiser les variables de chaîne dans Bash

vote vote

94

foo="Hello" foo="${foo} World" echo "${foo}" > Hello World 
a='Hello' b='World' c="${a} ${b}" echo "${c}" > Hello World 
vote vote

80

A="X Y" A+=" Z" echo "$A" 
vote vote

73

a=2 a+=4 echo $a 24 
foo="Hello" foo+=" World" echo $foo Hello World 
echo $a 24 ((a+=12)) echo $a 36 
echo ${a[@]} 36  a+=(18)  echo ${a[@]} 36 18 echo ${a[0]} 36 echo ${a[1]} 18 
a+=(one word "hello world!" ) bash: !": event not found 
a+=(one word "hello world"! 'hello world!' $'hello world\041')  declare -p a declare -a a='([0]="36" [1]="18" [2]="one" [3]="word" [4]="hello world!" [5]="h ello world!" [6]="hello world!")' 
echo ${a[@]} 36 18 one word hello world! hello world! hello world! 
printf -v a "%s./.%s...'%s' '%s', '%s'=='%s'=='%s'" "${a[@]}" echo $a 36./.18...'one' 'word', 'hello world!'=='hello world!'=='hello world!' 
declare -p a declare -a a='([0]="36./.18...'\''one'\'' '\''word'\'', '\''hello world!'\''=='\ ''hello world!'\''=='\''hello world!'\''" [1]="18" [2]="one" [3]="word" [4]="hel lo world!" [5]="hello world!" [6]="hello world!")' 
a=36 declare -p a declare -a a='([0]="36" [1]="18" [2]="one" [3]="word" [4]="hello world!" [5]="he llo world!" [6]="hello world!")' 
printf -v a[0] '<%s>\n' "${a[@]}" echo "$a" <36> <18> <one> <word> <hello world!> <hello world!> <hello world!> 
foo="Hello" printf -v foo "%s World" $foo echo $foo Hello World 
printf -v foo "%s World" "$foo" 
foo="Hello" foo="$foo World" echo $foo Hello World 
foo="Hello" foo=$(printf "%s World" "$foo") echo $foo Hello World 
foo="Hello" foo=`printf "%s World" "$foo"` echo $foo Hello World 
foo="Today is: " foo=$(printf "%s %s" "$foo" "$(date)") echo $foo Today is: Sun Aug 4 11:58:23 CEST 2013 
foo="Today is: " foo=`printf "%s %s" "$foo" "\`date\`"` echo $foo Today is: Sun Aug 4 11:59:10 CEST 2013 
vote vote

66

$ var="myscript"  $ echo $var  myscript   $ var=${var}.sh  $ echo $var  myscript.sh 
vote vote

51

bla=hello laber=kthx echo "${bla}ohai${laber}bye" 
helloohaikthxbye 

Questions similaires