-1

i'm confused about the $symbol for unix. according to the definition, it states that it is the value stored by the variable following it. i'm not following the definition - could you please give me an example of how it is being used?

thanks

4

2 回答 2

3

You define a variable like this:

greeting=hello
export name=luc

and use like this:

echo $greeting $name

If you use export that means the variable will be visible to subshells.

EDIT: If you want to assign a string containing spaces, you have to quote it either using double quotes (") or single quotes ('). Variables inside double quotes will be expanded whereas in single quotes they won't:

axel@loro:~$ name=luc
axel@loro:~$ echo "hello $name"
hello luc
axel@loro:~$ echo 'hello $name'
hello $name
于 2013-03-09T09:00:20.087 回答
1

在外壳脚本的情况下。为变量赋值时,不需要使用 $ simbol。仅当您想访问该变量的值时。例子:

     VARIABLE=100000;
     echo "$VARIABLE";
     othervariable=$VARIABLE+10;
     echo $othervariable;

另一件事:如果您使用 assignment ,则 = simbol 前后不要留空格。

这是一个很好的 bash 教程: http: //linuxconfig.org/Bash_scripting_Tutorial

我的名字.sh:

     #!/bin/sh
     finger | grep "`whoami` " | tail -n 1 | awk '{FS="\t";print $2,$3;}'

finger:打印所有登录的用户示例结果:

    login   Name              Tty      Idle  Login Time   Office     Office Phone
    xuser   Forname Nickname  tty7     3:18  Mar  9 07:23 (:0)
    ...

grep:过滤包含给定字符串的行(在本例中,如果我们的登录名是 xuser,我们需要过滤 xuser) http://www.gnu.org/software/grep/manual/grep.html

whoami:打印我的登录名 http://linux.about.com/library/cmd/blcmdl1_whoami.htm

tail -n 1 :仅显示最后一行结果 http://unixhelp.ed.ac.uk/CGI/man-cgi?tail

awk 脚本:打印结果的第二列和第三列:Forname, Nickname http://www.staff.science.uu.nl/~oostr102/docs/nawk/nawk_toc.html

于 2013-03-09T09:01:26.867 回答