whataday
whataday

Reputation: 47

a bash command generates strange behavior

I am learning bash and happened to type var = "$(cat)" in bash, the strange thing is it enters and bash prompt disappeared until I use ctrl-c.

what is the command var = "$(cat)"?

bash-3.2$ var = "$(cat)"

^C
bash-3.2$

Upvotes: 1

Views: 59

Answers (1)

danrodlor
danrodlor

Reputation: 1449

Writing var=$(cat) means that you're trying to store the output of a subshell that executes the command cat. However, executing cat without arguments is equivalent to cat STDIN (also the same as cat -), and if you don't properly terminate the input flow, cat will still read from STDIN until it is interrupted (that's why you think your prompt disappeared but, actually, you're in the subshell).

Since you're sending the SIGINT signal (CTRL+C) to the (sub)process, the command, and thus the subshell, exits with a non-0 status (you can check the exit status executing echo $? just after var=$(cat), it should be equal to 130 for a process terminated by SIGINT). Alternatively, you can try to write something to the STDIN of the aforementioned subshell and then send CRTL+D, which when typed at the start of a line on a given terminal signifies the end of the input, instead of CTRL+C. Finally, you can type echo $var in order to check if the variable assignment did work as expected.

Upvotes: 1

Related Questions