Writing Quick Scripts with Cat Command From The Bash Prompt

Have you ever been working with the bash prompt and want to write a short script without having to open vi or any other editor? Well, you can do that. Just use the command:

cat > scriptname.sh

and write your script. When your done writing just press enter and “Ctrl+c”
here’s an example I use to open the android adv manager.

jorge@nixlap ~ $ cat > startavd
#!/bin/bash
#script to start android virtual device mangaer
 
cd ~/android-sdk-linux/tools 
 
if [ $? -eq 0 ]; then
	./android avd
fi
 
#end
^C

Also, you can append text to an existing file. Let’s say you have the following file

jorge@NIXBOXEN:~$ cat file
one
two
three
four
five

and you want to do a quick edit and add some text to the file, you could use the “>>” redirect to append text

jorge@NIXBOXEN:~$ cat >> file
1
3
4
5
6
^C
jorge@NIXBOXEN:~$ 
jorge@NIXBOXEN:~$ cat file
one
two
three
four
five
1
3
4
5
6
Share This!

Leave a Reply

Your email address will not be published.