Skip to content
Advertisement

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.

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 | source ~/.bash_profile

As I expected, I can see the OS prompt instead of seeing Shell wait the user input. However, I noticed that the last line exporting TEST6 variable doesn’t get exported. I mean I can’t find the value of the variable TEST6.

Can you let me know what I am missing?

Advertisement

Answer

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. (All here strings implicitly end with a newline.)

If your shell does not support <<<, you can use a POSIX-standard here document instead.

. ~/some_file <<EOF
EOF
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement