参数在shell脚本中传递时发生更改

参数在shell脚本中传递时发生更改,shell,alias,ksh,Shell,Alias,Ksh,我使用的是ksh外壳。我必须从定义的别名获取信息,我使用下面的代码 #!/usr/bin/ksh source ~/.profile echo "parameter : $1" alias | grep hello 我已将上述脚本保存为test.sh 或者运行test.sh,我得到以下输出: ./test 324 parameter : autologout hello='Hello, How are you' 我的问题是,当我将324作为参数传递时,为什么autologout的值是$1?

我使用的是ksh外壳。我必须从定义的别名获取信息,我使用下面的代码

#!/usr/bin/ksh
source ~/.profile
echo "parameter : $1"
alias | grep hello
我已将上述脚本保存为test.sh 或者运行test.sh,我得到以下输出:

./test 324 
parameter : autologout
hello='Hello, How are you'
我的问题是,当我将324作为参数传递时,为什么autologout的值是
$1

如何解决此问题?

您只需在执行
源代码之前保存参数
$1
,这可能会更改位置参数

#!/usr/bin/ksh

arg1="$1"
source ~/.profile
echo "parameter : $arg1"
alias | grep hello

在执行
source
之前,只需保存参数
$1
,这可能会更改位置参数

#!/usr/bin/ksh

arg1="$1"
source ~/.profile
echo "parameter : $arg1"
alias | grep hello

确保~/.profile中没有
设置
命令

#!/usr/bin/ksh
source ~/.profile
set "new"
echo "parameter : $1"
alias | grep hello

>./script.ksh foo    
parameter : new
现在,您的参数将始终为“新”。
请尝试注释掉
source
命令,查看错误是否仍然存在。如果是,则在“~/.profile”中搜索
set
命令

确保您的~/.profile中没有
set
命令

#!/usr/bin/ksh
source ~/.profile
set "new"
echo "parameter : $1"
alias | grep hello

>./script.ksh foo    
parameter : new
现在,您的参数将始终为“新”。 请尝试注释掉
source
命令,查看错误是否仍然存在。如果是,则在“~/.profile”中搜索
set
命令