How to create a file and insert a line in it using OS X terminal

How can I do the following using the terminal on OS X?

Create a file named .inputrc in a user's home directory and put this line in it:

set completion-ignore-case On


Quick Answer

touch ~/.inputrc
echo "set completion-ignore-case On" >> ~/.inputrc

Explanation

First, create the file:

touch ~/.inputrc

Then, add the contents to the file:

echo "set completion-ignore-case On" >> ~/.inputrc

touch creates an empty file (assuming that the ~/.inputrc file does not already exist). echo writes text to the "standard output" ("stdout" for short), which is normally your screen, but because of the redirection (>>), the output is redirected to ~/.inputrc. This setup will append the line to the file.

If ~/.inputrc already exists and you want to erase (clobber) its contents, and then write the line into it (i.e., create a file with only this line of text), do:

echo "set completion-ignore-case On" > ~/.inputrc

The single arrow (>), a.k.a. "greater than" symbol, tells echo to create a file with only the given text as the contents of the file, instead of writing the contents to the end of the file. (Actually, echo does not create the file; the shell creates the file, discarding any existing contents, and the echo command writes the new contents.)


If you use the first approach (with the >>) and you find that the line that you added is smushed onto the previous line, e.g.,

some stuff here
some more stuff hereset completion-ignore-case On

then you need to edit the file to fix it. This would happen if the last line of the pre-existing file ended with a textual character rather than a "newline" character (i.e., an end-of-line marker). This is common for .TXT files on Windows, but rare on *nix.

If you somehow realize in advance that your .inputrc file has pre-existing contents that do not end with a newline, then you should use this echo statement instead:

echo -e "\nset completion-ignore-case On" >> ~/.inputrc

The \n before the phrase is interpreted as a newline character, so a newline is added after the previous contents and before the new stuff you want to add.

Or, slightly more typing but much more readable,

echo "" >> ~/.inputrc
echo "set completion-ignore-case On" >> ~/.inputrc

or

(echo ""; echo "set completion-ignore-case On") >> ~/.inputrc

which do the same thing; i.e., provide the missing newline character to the existing text, and then add the set completion-… command after that.


All you need to do is:

echo "set completion-ignore-case On" >> ~/.inputrc 

echo simply echos the text given to it through the normal output channel (stdout)

the >> writes the stdout output from the left hand command to the right hand file, which in your case is ~/.inputrc

~/ is the same as /home/your_username/