Using aliases with nohup

不打扰是莪最后的温柔 提交于 2019-12-04 02:19:59

Because the shell doesn't pass aliases on to child processes (except when you use $() or ``).

$ 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.

$ sayHello 

Hello world!

Since you said "sayHello" in the same shell it worked.

$ 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.

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.

In your ~/.bash_profile add: export PATH="/my/shiny/interpreters/bin:${PATH}"

Then relogin.

Since this is an environment variable, it will be passed to all the child processes, be they shells or not - it should now work very often.

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.

With bash, you can invoke a subshell interactively using the -i option. This will source your .bashrc as well as enable the expand_aliases shell option. Granted, this will only work if your alias is defined in your .bashrc which is the convention.

Bash manpage:

If the -i option is present, the shell is interactive.

expand_aliases: If set, aliases are expanded as described above under ALIASES. This option is enabled by default for interactive shells.

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.


$ nohup bash -ci 'sayHello'

If you look at the Aliases section of the Bash manual, it says

The first word of each simple command, if unquoted, is checked to see if it has an alias.

Unfortunately, it doesn't seem like bash has anything like zsh's global aliases, which are expanded in any position.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!