0

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
1
  • 1
    These type of questions are frequently asked in this site.You may want to check them before asking a question. Commented Jun 30, 2018 at 14:47

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.