Cygwin 中的 bash shell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17176385/
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
bash shell in Cygwin
提问by user2201373
when I am in a Cygwin terminal, I can easily use the "source" command.
当我在 Cygwin 终端中时,我可以轻松使用“源”命令。
For example, let's say I have a file called "my_aliases.sh", that contains the following
例如,假设我有一个名为“my_aliases.sh”的文件,其中包含以下内容
#!/bin/bash -f
alias clear='cmd /c cls'
#unalias clear
Then on the Cygwin terminal, I can type
然后在 Cygwin 终端上,我可以输入
$source my_aliases.sh
And it just works fine, and whenever I type "clear", I can see that it works.
它运行良好,每当我输入“clear”时,我都能看到它运行良好。
But I don't know why doing the same thing inside another shell script, and calling that shell script doesn't work.
但我不知道为什么在另一个 shell 脚本中做同样的事情,调用那个 shell 脚本不起作用。
For example, let's say that I have a file called "run_alias.sh", with the following content:
例如,假设我有一个名为“run_alias.sh”的文件,其内容如下:
#!/bin/bash -f
#
a=`source my_aliases.sh`
b=`ls -ltr`
echo $a
echo $b
And when I try to run my file
当我尝试运行我的文件时
$ ./run_alias.sh
It just doesn't do anything. For example, I can see that the command (b) takes place, but nothing happens for command (a).
它只是什么都不做。例如,我可以看到命令 (b) 发生了,但命令 (a) 没有任何反应。
But after I run "run_alias.sh", and type "clear", I get the following error:
但是在我运行“run_alias.sh”并输入“clear”后,出现以下错误:
$ clear
bash: clear: command not found
I even tried to change run_alias.sh as follows:
我什至尝试更改 run_alias.sh 如下:
#!/bin/bash -f
echo `source my_aliases.sh`
But now when run run_alias.sh, and type clear, I get the exact same error message !!!
但是现在当运行 run_alias.sh 并输入 clear 时,我得到完全相同的错误消息!!!
Any idea how to call the "source" command from some other shell script in Cygwin?
知道如何从 Cygwin 中的其他 shell 脚本调用“源”命令吗?
回答by glenn Hymanman
A child process cannot alter its parent's environment.
子进程不能改变其父进程的环境。
When you execute the run_alias.sh script, you launch a new bash process, which sources your alias file. Then the script ends, that bash process terminates and it takes its modified environment with it.
当您执行 run_alias.sh 脚本时,您将启动一个新的 bash 进程,该进程提供您的别名文件。然后脚本结束,该 bash 进程终止并带走其修改后的环境。
If you want your aliases to be automatically available, source it from your $HOME/.bashrc file.
如果您希望您的别名自动可用,请从您的 $HOME/.bashrc 文件中获取它。
回答by mob
Backticks create a subshell. The changes made to your environment in that subshell do not affect the calling environment.
反引号创建一个子shell。在该子 shell 中对您的环境所做的更改不会影响调用环境。
Id you want your script (run_alias.sh) to have access to the environment in my_aliases.sh, call sourcedirectly.
如果您希望您的脚本 ( run_alias.sh) 能够访问 中的环境my_aliases.sh,请source直接调用。
source my_aliases.sh
b=`ls -lrt`
echo $b
and if you want the changes that run_alias.shmakes to its environment to propagate to it's parent, run sourceon the command line.
如果您希望run_alias.sh对其环境所做的更改传播到其父级,请source在命令行上运行。
$ source run_alias.sh

