一分钟学会变量,别名,历史命令,一行上执行两个命令,引号的使用

简介: 变量简单说就是让某一个特定字符串代表不固定的内容,变量可分为两类:环境变量(全局变量)和普通变量(局部变量

变量

变量简单说就是让某一个特定字符串代表不固定的内容,变量可分为两类:环境变量(全局变量)和普通变量(局部变量

创建普通变量local_data=1并访问

[root@ls_nfqZ8Onc ~]# local_data=1
[root@ls_nfqZ8Onc ~]# echo $local_data 
1


创建环境变量ROOT_DATA=root, 只有root用户可以访问到

[root@ls_nfqZ8Onc ~]# vi /root/.bashrc
[root@ls_nfqZ8Onc ~]# source /root/.bashrc 
[root@ls_nfqZ8Onc ~]# echo $ROOT_DATA
root


创建环境变量USER_DATA=user, 只有普通用户可以访问到

[user1@ls_nfqZ8Onc ~]$ export USER_DATA=user
[user1@ls_nfqZ8Onc ~]$ echo $USER_DATA
user


创建环境变量DATA=all, root用户和普通用户都可以访问到

[user1@ls_nfqZ8Onc ~]# vi /etc/profile
[user1@ls_nfqZ8Onc ~]# source /etc/profile
[user1@ls_nfqZ8Onc ~]# echo $DATA
all,root


别名

创建3个文件test1.txt, test2.txt, test3.txt

[root@ls_nfqZ8Onc ~]# touch test1.txt, test2.txt, test3.txt
[root@ls_nfqZ8Onc ~]# ls
file       file3.txt  file.tar.bz2  Linux.txt        test1.txt,
file1.txt  file4      file.tar.gz   tar_file.tar.gz  test2.txt,
file2.txt  file4.txt  file.tar.xz   tar_test         test3.txt

使用find查找test1.txt,test2.txt, test3.txt

[root@ildkbkbpswtafnnq ~]# touch test1.txt test2.txt test3.txt
[root@ildkbkbpswtafnnq ~]# ls
test1.txt  test2.txt  test3.txt
[root@ildkbkbpswtafnnq ~]# find test[1-3].txt
test1.txt
test2.txt
test3.txt


使用别名: 将上边命令命名为myfind

取消别名

[root@ildkbkbpswtafnnq ~]# alias myfind=find
[root@ildkbkbpswtafnnq ~]# myfind test[1-3].txt
test1.txt
test2.txt
test3.txt
[root@ildkbkbpswtafnnq ~]# unalias myfind
[root@ildkbkbpswtafnnq ~]# myfind test[1-3].txt
-bash: myfind: command not found
[root@ildkbkbpswtafnnq ~]# find test[1-3].txt
test1.txt
test2.txt
test3.txt

历史命令

查看最近使用的10条历史命令

[root@ildkbkbpswtafnnq ~]# history 10
   10  touch test3.txt
   11  find test[1-3].txt
   12  ls
   13  find test[1-3].txt
   14  alias myfind=find
   15  myfind test[1-3].txt
   16  unalias myfind
   17  myfind test[1-3].txt
   18  find test[1-3].txt
   19  history 10


在一行上执行两个命令,打印123和从root切换到普通用户

[root@ls_nfqZ8Onc ~]# echo 123;su - user1
123
Last login: Tue Jul 19 19:57:12 CST 2022 on pts/1
[user1@ls_nfqZ8Onc ~]$ 


引号的使用举例: 无引号,单引号,双引号,反引号,$()

无引号

[user1@ls_nfqZ8Onc ~]$ echo 123
123


单引号

[user1@ls_nfqZ8Onc ~]$ echo '$data'
$data


双引号

[user1@ls_nfqZ8Onc ~]$ echo "$data"
1


反引号

反引号的作用就是将反引号内的Linux命令先执行,然后将执行结果赋予变量。尽管可以通过输入字符或者字符串来创建变量值,也可以获取来自于其他Linux命令的值。为把Linux命令的结果赋予变量,实现需要执行这个命令。如果在命令行上把Linux命令放在反引号中,这个命令会首先被执行,其结果会成为命令行的一个参数。在赋值时,通过把命令放在反引号中,以便于首先执行,命令的执行结果会被赋予一个变量。反引号可以被视为由要执行命令组成的表达式,其结果会被赋予变量。组成命令的字符本身不会被赋予。在下面的范例中,命令ls 被执行,其结果然后被赋予变量listc。ls会生成所有文件列表。这个文件列表随后被赋予变量listc。

[root@ls_nfqZ8Onc ~]# listc=`ls` 
[root@ls_nfqZ8Onc ~]# echo $listc 
file file1.txt file2.txt file3.txt file4 file4.txt file.tar.bz2 file.tar.gz file.tar.xz Linux.txt tar_file.tar.gz tar_test test1.txt, test2.txt, test3.txt


( ) 在 b a s h 中, () 在bash中,()在bash中,( )与(反引号)都是用来作命令替换的。 命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组成新的命令行。

[root@ls_nfqZ8Onc ~]# echo today is $(date "+%Y-%m-%d")
today is 2022-07-19


相关文章
|
2月前
|
Shell 数据处理
Bash 中检查文件是否包含字符串
【8月更文挑战第27天】
30 5
|
12月前
makefile 变量的替换,嵌套引用,命令行变量
makefile 变量的替换,嵌套引用,命令行变量
86 1
|
5月前
|
Shell Perl
几行shell,批量替换更改件名
几行shell,批量替换更改件名
21 1
|
5月前
|
人工智能 机器人 Shell
【shell】shell数组的操作(定义、索引、长度、获取、删除、修改、拼接)
【shell】shell数组的操作(定义、索引、长度、获取、删除、修改、拼接)
|
运维 Shell C语言
shell脚本介绍、结构和执行、变量及date命令用法
shell脚本介绍、结构和执行、变量及date命令用法
219 2
|
SQL 关系型数据库 MySQL
从“表”中删除为“别名” ...在“别名列”中...为什么语法错误?
从“表”中删除为“别名” ...在“别名列”中...为什么语法错误?
|
Linux
LINUX脚本传递多个带空格参数的解决办法
LINUX脚本传递多个带空格参数的解决办法
344 0
|
Shell
Shell数组变量定义-获取-拼接-删除
Shell数组变量定义-获取-拼接-删除
144 0