Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just started using bash scripting language and having trouble understanding "command substitution " and " redirection". I am using bash man very frequently and know something about this specific subject.

Here is an example of using command substitution.

I used command "sed" to find line numbers, in file, where pattern "choice" match.
As usual, "sed" outputs to stdout, OR the variable / file "index" can be "retrieved" by using command substitution. Which I did and it works as expected.

index=$(sed -n "/$choice/=" "$PWD$DEBUG_DIR$DEBUG_MENU")


I am still not too comfortable using redirection, however, I like to know if there is any particular advantage using either one of these methods to accomplish the task.

What I have tried:

Yes, I did try "redirection" but it did not work as expected.

sed -n "/$choice/=" "$PWD$DEBUG_DIR$DEBUG_MENU" >> index
Posted
Updated 21-Sep-18 22:15pm

1 solution

OK, to continue from my earlier message:

1. "Yes, I did try "redirection" but it did not work as expected."
This is one of the most frustrating comments that we see in a question. We have no idea what you did expect to see or happen. And we have no idea what did happen. Without a clear explanation of what you expected and what result you received we cannot really help.

2. You are mixing command substitution and redirection, so it is not clear which is the big problem. Redirection is quite straightforward, it allows you to send the output of a command to a file instead of the console, so you can read it later in your editor. It also allows you to send the contents of a file as input to a command. So a command like:
ls -l > foo.txt

sends the output from the list command to the file foo.txt. You can also use the pipe | to send the output from one command as the input to another. Thus
ls -l | grep "foo"

sends the output of the ls command as input to grep, which will list all files that contain the string 'foo'.

3. As to command substitution, that uses the construct $(command) which runs command and replaces the $(command) in the script with the actual result of the command. So in your statement above
index=$(sed -n "/$choice/=" "$PWD$DEBUG_DIR$DEBUG_MENU")

you are setting the local variable index, to the result of the statements in the parentheses. Which is not the same as piping it into a file.
 
Share this answer
 
v2
Comments
Vaclav_ 22-Sep-18 9:33am    
I asked for explanation when to use command substitution or redirection.
Since everything in Linux is a file, I assume I can "redirect" from stdout to VARIABLE - in this case "index".
I did specify that I did attempted to do so.
I will eventually figure out how.
Richard MacCutchan 22-Sep-18 10:45am    
And that is what I gave you. If you want to send your output to a file then you use redirection. If you want to use the output as part of a compound statement then you may use command substitution. And everything in Linux is not a file; no idea where you got that from.

As I mentioned above, your statement
index=$(sed -n "/$choice/=" "$PWD$DEBUG_DIR$DEBUG_MENU")
does not create a file, it creates a local environment variable called index which contains the result of the command in parentheses.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900