2021年1月5日 星期二

Linux Shell Script 保留/系統預設的變數 (轉貼)

 

保留/系統預設的變數

系統預設已經有相當多的變數定義了, 因此在你的shell script裡面 要去避免這些變數. 以下就是一些預設的系統變數. 

$BASH_ENV absolute path of startup file
$CDPATH directories searched by cd
$FCEDIT absolute path of history editor
$HISTCMD the history number of the current command
$HISFILE absolute path of history file
$HISTSIZE number of remembered commands
$HOME login directory
$IFS token delimiters
$LINENO current line number in shell script
$LINES terminal height
$MAIL  absolute path of mailbox
$MAILCHECK number of seconds to check mail
$OLDPWD absolute path of previous directory
$OPTARG option set by getopt
$OPTIND option's ordinal position set by getopt
$OSTYPE the OS on which bash is executing
$PATH command search path
$PPID  process ID of parent
$PS1  primary prompt
$PS2 secondary prompt
$PWD absolute path of current directory
$RANDOM random integer
$REPLY default variable for read
$SECONDS number of seconds since shell started
$SHELL absolute pathname of preferred shell
$TMOUT seconds to log out after lack of use
$UID user ID of the current user
$$ process ID of current shell
$? exit status of most recent statement
$# 參數的數目
$* 代表所有參數
$! PID of the most recently started backgroup job


另外, 還有參數的傳遞也需要了解, 例如當我們編寫一個 script 內容如下:

#!/bin/bash
echo "\$0 ==> $0"
echo "\$1 ==> $1"
echo "\$2 ==> $2"

則執行以下命令的時候:

./myscript Hello World
$0 ==> ./myscript
$1 ==> Hello
$2 ==> World


You can use $(!!) to recompute (not re-use) the output of the last command.

The !! on its own executes the last command.

$ echo pierre
pierre
$ echo my name is $(!!)
echo my name is $(echo pierre)
my name is pierre

沒有留言:

張貼留言