当前位置: 首页 > Linux

LinuxShell变量

时间:2023-04-07 00:27:17 Linux

定义变量#等号两边不能有空格url=http://www.qq.com/shell/name='我的网站'author="wu"使用变量#推荐所有变量上花括号{}echo$authorecho${author}echo"我擅长${skill}脚本"#建议左右用引号防止格式混淆LSL=`ls-l`echo"${LSL}"修改变量url的值="http://www.qq.com"url="http://www.qq.com/shell/"单引号和双引号的区别#!/bin/bashurl="http://www.qq.com"website1='我的网址:${url}'website2="我的网址:${url}"#我的网址:${url}echo$website1#我的网址:http://www.qq.comecho$website2将命令的结果赋值给变量格式:variable=`command`variable=$(command)log=$(catlog.txt)echo$log#!/bin/bashbegin_time=`date`#开始时间,用``代替sleep20sfinish_time=$(date)#结束时间,用$()代替echo"Begintime:$begin_time"echo"Finishtime:$finish_time"#!/bin/bashbegin_time=`date+%s`#开始时间,用``代替sleep20sfinish_time=$(date+%s)#结束时间,用$()代替run_time=$((finish_time-begin_time))#Time区别echo"开始时间:$begin_time"echo"结束时间:$finish_time"echo"运行时间:${run_time}s"#使用$()支持嵌套,反引号不支持Fir_File_Lines=$(wc-l$(ls|sed-n'1p'))echo"$Fir_File_Lines"需要注意的是,$()只在BashShell中有效,而反引号可以在多个shell中使用,所以这两种命令替换方式各有特点,选择哪种方式就看个人需要了。删除变量#!/bin/shmyUrl="http://www.qq.com/shell/"unsetmyUrlecho$myUrl参考:http://c.biancheng.net/view/1...