bash 在 nohup 中使用别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9301531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using aliases with nohup
提问by asf107
Why doesn't the following work?
为什么以下不起作用?
$ alias sayHello='/bin/echo "Hello world!"'
$ sayHello
Hello world!
$ nohup sayHello
nohup: appending output to `nohup.out'
nohup: cannot run command `sayHello': No such file or directory
(the reason I ask this question is because I've aliased my perland pythonto different perl/python binaries which were optimized for my own purposes; however, nohup gives me troubles if I don't supply full path to my perl/python binaries)
(我问这个问题的原因是因为我已经将我的perl和python不同的 perl/python 二进制文件别名化,这些文件针对我自己的目的进行了优化;但是,如果我不提供我的 perl/python 二进制文件的完整路径,nohup 会给我带来麻烦)
采纳答案by Danny Milosavljevic
Because the shell doesn't pass aliases on to child processes (except when you use $() or ``).
因为 shell 不会将别名传递给子进程(除非您使用 $() 或 ``)。
$ alias sayHello='/bin/echo "Hello world!"'
$ alias sayHello='/bin/echo "Hello world!"'
Now an alias is known in this shell process, which is fine but only works in this one shell process.
现在在这个 shell 进程中知道了一个别名,这很好,但只在这个 shell 进程中有效。
$ sayHello
Hello world!
Since you said "sayHello" in the same shell it worked.
由于您在同一个 shell 中说“sayHello”,因此它起作用了。
$ nohup sayHello
Here, a program "nohup" is being started as a child process. Therefore, it will not receive the aliases. Then it starts the child process "sayHello" - which isn't found.
在这里,程序“nohup”作为子进程启动。因此,它不会收到别名。然后它启动子进程“sayHello” - 没有找到。
For your specific problem, it's best to make the new "perl" and "python" look like the normal ones as much as possible. I'd suggest to set the search path.
对于您的具体问题,最好尽可能使新的“perl”和“python”看起来像正常的。我建议设置搜索路径。
In your ~/.bash_profile add: export PATH="/my/shiny/interpreters/bin:${PATH}"
在你的 ~/.bash_profile 添加:export PATH="/my/shiny/interpreters/bin:${PATH}"
Then relogin.
然后重新登录。
Since this is an environment variable, it willbe passed to all the child processes, be they shells or not - it should now work very often.
由于这是一个环境变量,它会被传递到所有的子进程,无论是弹还是没有-它现在应该很经常工作。
回答by chetan patel
For bash: Try doing nohup 'your_alias'. It works for me. I don't know why back quote is not shown. Put your alias within back quotes.
对于 bash:尝试执行 nohup ' your_alias'。这个对我有用。我不知道为什么没有显示反引号。将您的别名放在反引号内。
回答by Travis Clarke
With bash, you can invoke a subshell interactively using the -ioption. This will source your .bashrcas well as enable the expand_aliasesshell option. Granted, this will only work if your aliasis defined in your .bashrcwhich is the convention.
使用 bash,您可以使用该-i选项以交互方式调用子 shell 。这将提供您.bashrc以及启用expand_aliasesshell 选项。当然,这仅在您的别名在您.bashrc的约定中定义时才有效。
Bash manpage:
Bash 联机帮助页:
If the
-ioption is present, the shell is interactive.expand_aliases: If set, aliases are expanded as described above under ALIASES. This option is enabled by defaultfor interactive shells.
When an interactive shell that is not a login shell is started, bash reads and executes commands from
/etc/bash.bashrcand~/.bashrc, if these files exist.
如果该
-i选项存在,则外壳是交互式的。expand_aliases:如果设置,别名将按照上面在 ALIASES 下的描述进行扩展。对于交互式 shell ,默认情况下启用此选项。
当一个不是登录 shell 的交互式 shell 启动时,bash 读取并执行来自
/etc/bash.bashrc和 的命令~/.bashrc(如果这些文件存在)。
$ nohup bash -ci 'sayHello'
回答by jjlin
If you look at the Aliasessection of the Bash manual, it says
如果您查看Bash 手册的别名部分,它会说
The first word of each simple command, if unquoted, is checked to see if it has an alias.
每个简单命令的第一个单词,如果没有被引用,将检查它是否有别名。
Unfortunately, it doesn't seem like bashhas anything like zsh's global aliases, which are expanded in any position.
不幸的是,它似乎bash没有像zsh's global aliases这样的东西,可以在任何位置扩展。

