bash shell $HOME 赋值和脚本执行

Posted

技术标签:

【中文标题】bash shell $HOME 赋值和脚本执行【英文标题】:bash shell $HOME assignment and script execution 【发布时间】:2012-02-15 11:21:41 【问题描述】:

我刚刚开始学习 Unix,到目前为止遇到了两个基本但难以解决的问题:

当我在 shell 脚本中将HOME='' 设置为指定目录时,当前 目录似乎不再被识别。也就是说,'cd ~/' 会吐出消息:'no such file or directory' 消息。尽管奇怪的是,如果在脚本中进行了别名分配,源调用似乎仍然激活了它们。怎么会?

例如:

$ more .profile
HOME="~/Documents/Basics/Unix/Unix_and_Perl_course"
cd $HOME
[...]
$ source .profile
-bash: cd: ~/Documents/Basics/Unix/Unix_and_Perl_course: No such file or directory
当我通过 nano ('hello.sh') 创建一个简单的 shell 脚本时,我似乎无法通过在终端中键入 'hello.sh' 来执行它。即使在我 'chmod +x' 文件后,此问题也无法解决。有什么问题?

例如:

$ more hello.sh 
# my first Unix shell script
echo "Hello World"
$ hello.sh
bash: hello.sh: command not found

谢谢!

【问题讨论】:

这里有两个不同的问题。最好分别问他们。最好给出一个有意义的标题,让问题对未来的读者更有用。 感谢您的建议,迈克尔!我会记住的。 【参考方案1】:

您也不想“重载”$HOME,HOME 的默认位置始终是您的主目录。如果你搞砸了,很多东西都会坏掉。

至于 hello.sh - 那是因为你没有 '.'在你的 $PATH 中。 (这是好事)

试试:

./hello.sh

如果它说它不能执行

chmod 755 hello.sh
./hello.sh

【讨论】:

【参考方案2】:
    ~ = $HOME 。 (pwd) 不在 $PATH 中

【讨论】:

以上是关于bash shell $HOME 赋值和脚本执行的主要内容,如果未能解决你的问题,请参考以下文章

linux定时任务shell脚本开头如下 cd ~ . .bash_profile 定时任务执行后提示找不到.bash_profile啥情况

shell基础

shell基础

shell变量用grep怎么不能赋值

从 PHP 脚本执行 shell 脚本

Bash 脚本手动运行,但在 crontab 上失败