1. 程式人生 > >linux shell將命令結果賦值給變數 shell assign command output to variable

linux shell將命令結果賦值給變數 shell assign command output to variable

1. 命令

    1.1     反引號``    (也就是tab上面~鍵)

    
       ~ a=`echo "hello world"`
       ~ echo $a

         hello world


     1.2    $()

        a=$(echo "hello world")
        echo $a

        hello world


2.  例項  擷取目錄下某個檔名

2.1 需求  拿到目錄下時間最近的 以.csh 結尾的檔名(黃色部分)

bash-3.2$ ls -al -rt|grep .csh
-rw-r--r-- 1 testsybnike     692 Apr 11 23:42 iapkg_nike.env.180411.2340.csh
-rw-r--r-- 1 testsybnike     692 Apr 12 03:12 iapkg_nike.env.180412.0311.csh
-rw-r--r-- 1 testsybnike     692 Apr 12 07:13 iapkg_nike.env.180412.0712.csh
-rw-r--r-- 1 testsybnike     692 Apr 12 08:13 iapkg_nike.env.180412.0811.csh
-rw-r--r-- 1 testsybnike     692 Apr 12 09:07 iapkg_nike.env.180412.0906.csh

-rw-r--r-- 1 testsybnike     692 Apr 13 00:58 iapkg_nike.env.180413.0057.csh


2.2  code指令碼

#!/bin/csh -x

set cshFile=`ls -l *.csh| tail --lines=1|awk '{print $NF}'`

echo "csh File=$cshFile"

bash 下

test=$(ls -l *.csh| tail --lines=1|awk '{print $NF}')


2.3  csh $()報錯 

 set test=$(ls -l *.csh|tail --lines=1|awk '{print $NF}')

Illegal variable name.