My question is about file handling in bash. I want to write a string to text file in Bash script. I have already tried
echo string>>filename.txt
My question is about file handling in bash. I want to write a string to text file in Bash script. I have already tried
echo string>>filename.txt
your code is correct.you can write to a file using that.But you have to check the file access permission of the file you want to write the data in to. Check following for more details. Create text file and fill it using bash Open and write data to text file using bash/shell scripting
That is a valid way of doing it. I would however consider this:
echo "string" >> file.txt
The quotes make it possible to handle more than one word (in the case of echo, that works anyway, but it's good practice to always use it). You should also distinguish between > and >>
> truncates the file (erases it) and replaces its contents with your string
>> on the other hand appends your string to the bottom of your file