您的位置:首页 > 其它

将命令结果赋值给一个变量

2012-10-27 11:45 267 查看
#!/bin/bash

a=23              # Simple case
echo $a
b=$a
echo $b
echo

# Now, getting a little bit fancier (command substitution).

a=`echo Hello!`   # Assigns result of 'echo' command to 'a' ...
echo $a
#  Note that including an exclamation mark (!) within a
#+ command substitution construct will not work from the command-line,
#+ since this triggers the Bash "history mechanism."
#  Inside a script, however, the history functions are disabled.

a=`ls -l /home`         # Assigns result of 'ls -l' command to 'a'
echo $a           # Unquoted, however, it removes tabs and newlines.
echo
echo "$a"         # The quoted variable preserves whitespace.
# (See the chapter on "Quoting.")

exit 0


结果:

23

23

Hello!

total 8 drwxrwxrwx 15 root root 4096 2012-10-27 10:47 code drwxr-xr-x 4 user user 4096 2012-10-24 12:58 user

total 8

drwxrwxrwx 15 root root 4096 2012-10-27 10:47 code

drwxr-xr-x 4 user user 4096 2012-10-24 12:58 user
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: