Skip to content
Advertisement

Linux: Use parameter as file shortcut

Cheers everyone 🙂

I’m trying to make a linux script. This script shall be called with one parameter, a file stored in my home directory. I can’t seem to use

cat $var1 >> $1  

So i have this variable $var1 and I want to save it in a file that does exist and its name is given in $1

Anyone help me please!

Advertisement

Answer

The cat command shows the contents of a file.

Unless the value of $var1 is a file that you want to ‘copy’ to $1, it won’t work (probably gives a ‘file not found’ kind of error).

The easiest solution, I can think of, is to echo the variable:

echo "$var1" >> "$1"

As stated by @glglgl it is better to put the variable between double quotes. They prevent spaces messing up the command as they split a parameter into multiple parameters

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement