cancel
Showing results for 
Search instead for 
Did you mean: 
hedai710
Mission Specialist
Mission Specialist
  • 257 Views

RHCSA II Section 1 2.2 first bash script

Jump to solution

I have this exactly as it says on #2  but I don't get any return command from bash  firstscript.sh  //   is there a typo in the command provided or??? am I a Potatoe

Create and execute a simple Bash script.

  1. Use the vim command to create the firstscript.sh file under your home directory.

    [student@servera ~]$ vim firstscript.sh
  2. Insert the following text, and save the file. The number of hash signs (#) is arbitrary.

    #!/usr/bin/bash
    echo "This is my first bash script" > ~/output.txt
    echo "" >> ~/output.txt
    echo "#####################################################" >> ~/output.txt
  3. Use the bash command to execute the script.

    [student@servera ~]$ bash firstscript.sh
3 Solutions

Accepted Solutions
hedai710
Mission Specialist
Mission Specialist
  • 215 Views

Potatoe Confirmed ://   womp womp

View solution in original post

shashi01
Moderator
Moderator
  • 214 Views

@hedai710 

The script doesn’t “return” text to the screen; instead, it redirects output into a file. That’s why you only see the result if you open the file with cat output.txt.

 

echo "" → prints an empty string (basically nothing).

>> ~/output.txt → appends that nothing as a newline to the file.

View solution in original post

0 Kudos
shashi01
Moderator
Moderator
  • 211 Views

echo " "

echo prints whatever is inside the quotes.

"" is an empty string → so this prints nothing.

But echo always ends with a newline → so what actually gets sent is just a blank line.

>> ~/output.txt

>> means append to a file.

~/output.txt is the file in your home directory.

View solution in original post

0 Kudos
10 Replies
hedai710
Mission Specialist
Mission Specialist
  • 246 Views

hedai710_0-1758935602053.png

 

hedai710
Mission Specialist
Mission Specialist
  • 244 Views

hedai710_1-1758935824224.png

 

hedai710
Mission Specialist
Mission Specialist
  • 233 Views

I guess I was expecting a return command...  but the firstcript.sh is just putting  the echo "this is my first bash script into  output.txt  

what is the meaning of "" >> ~/output.txt

shashi01
Moderator
Moderator
  • 215 Views

@hedai710 

The script doesn’t “return” text to the screen; instead, it redirects output into a file. That’s why you only see the result if you open the file with cat output.txt.

 

echo "" → prints an empty string (basically nothing).

>> ~/output.txt → appends that nothing as a newline to the file.

0 Kudos
shashi01
Moderator
Moderator
  • 212 Views

echo " "

echo prints whatever is inside the quotes.

"" is an empty string → so this prints nothing.

But echo always ends with a newline → so what actually gets sent is just a blank line.

>> ~/output.txt

>> means append to a file.

~/output.txt is the file in your home directory.

0 Kudos
hedai710
Mission Specialist
Mission Specialist
  • 216 Views

Potatoe Confirmed ://   womp womp

hedai710
Mission Specialist
Mission Specialist
  • 207 Views

Noted, I think I was misunderstanding what the bash command actually does 

I reckon I thought it would print out the output as is  

so  bash  runs the script,  but the script itself  was just adding/appending the syntax into output.txt 

not sure why I thought bash  would print out  the script.... 

what ah marooon :///

shashi01
Moderator
Moderator
  • 196 Views

You’ve understood it correctly now. Bash doesn’t print the script itself it executes each command inside it. In your case, those echo commands don’t print directly to the screen because their output is being redirected (> or >>) into output.txt. That’s why nothing appeared in the terminal, but the text showed up when you checked the file.

So think of it this way: bash runs the script, the script runs commands, and redirection decides where the output goes (screen or file). You’re definitely on the right track now. This is exactly how scripting works

0 Kudos
Chetan_Tiwary_
Community Manager
Community Manager
  • 51 Views

@hedai710 The doubts that you had - thats exactly the kind of reasoning needed to truly understand a concept and make it stick in your mind forever! 

now in your script, notice the two redirection symbols :

1. ">"  which redirects output to a file, overwriting the file if it exists. 

2. ">>" Appends output to a file, preserving existing content. 

had you not use the output redirection , you would have got the expected output in your terminal. 

 

Now I will modify your script a little bit to give you both the results - one that the course step wants and the other what you want : 

Chetan_Tiwary__0-1759172284478.png

The tee -a command appends the output to both the terminal and the file.

 

0 Kudos
Join the discussion
You must log in to join this conversation.