Can someone please explain this? I ran the commands as shown below
$ cat `bash`
$ ls
$ ctrl+D
and it’s giving me some unexpected output on terminal.
NOTE: bash is in backquotes.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Good question! The “unexpected output” is cat printing all of the files found by ls in the cwd. Detailed explanation follow:
On your first line:
The
bashpart actually spawns a new shell from your original shell because bash is enclosed by backquotes (backquotes means to run the enclosed program in this context)Then when you do:
This is actually done in the newly spawned bash shell. It lists the directory of wherever the newly spawned bash shell is (should be the same as the original). This, in turn, in essence changes the cat command in the first step to
(basically all of the files in that directory returned by ls. However, you won’t see these results yet because the output is waiting to be printed to the stdout of your original shell : cat is waiting to evaluate the stdout of your new bash shell.)
Lastly, when you do:
It exits the new bash shell that you spawned from your original shell, and then cat outputs everything that got printed to stdout in the new shell (the search results from ls) into your old shell.
You can verify what I just said by:
Now run what you had in your question:
And this is what you should see:
in some order, which is just cat outputting all of the files that were found by ls.