简体   繁体   English

在Linux bash中模拟ENTER键并一起导出另一个变量

[英]Simulating ENTER key and export another variable together in Linux bash

I should put ENTER key value as a shell input to avoid shell waiting for user input. 我应该将ENTER键值作为shell输入,以避免shell等待用户输入。

echo 'enter'
read FRUIT

case "$FRUIT" in
   "apple") echo "Apple pie is quite tasty." 
   ;;
   "banana") echo "I like banana nut bread." 
   ;;
   "kiwi") echo "New Zealand is famous for kiwi." 
   ;;
esac

export TEST6=TEST

I have learned that 'echo' implicitly has an ENTER value, so tried to run below command to accomplish my requirement. 我了解到“ echo”隐式具有ENTER值,因此尝试在命令下运行以实现我的要求。

echo | source ~/.bash_profile

As I expected, I can see the OS prompt instead of seeing Shell wait the user input. 如我所料,我可以看到操作系统提示,而不是看到Shell等待用户输入。 However, I noticed that the last line exporting TEST6 variable doesn't get exported. 但是,我注意到导出TEST6变量的最后一行没有导出。 I mean I can't find the value of the variable TEST6. 我的意思是我找不到变量TEST6的值。

Can you let me know what I am missing? 你能让我知道我在想什么吗?

The value is being exported in a subshell run by the pipeline. 该值将在管道运行的子外壳中导出。 Use input redirection instead. 请改用输入重定向。

source ~/.bash_profile <<< ""

Any string could be used, since you don't appear to care about the actual value used, but an empty string is the equivalent of what echo with no arguments produces. 可以使用任何字符串,因为您似乎并不关心所使用的实际值,但是空字符串等效于不带参数的echo产生的内容。 (All here strings implicitly end with a newline.) (此处所有字符串隐式以换行符结尾。)

If your shell does not support <<< , you can use a POSIX-standard here document instead. 如果您的外壳不支持<<< ,则可以改用POSIX标准的here文档。

. ~/some_file <<EOF
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM