I was wondering if there is any method in Linux to read a string from the user, store it in a variable and then insert the variable(entire string) into a text file.
2 Answers
There are probably many ways to do it but I typically do something like this:
read usr_in
echo $usr_in > usr_input
read stores whatever is typed next in the variable usr_in. echo then prints/redirects the value of usr_in to the text file usr_input
Use >> instead of > if you want to append to rather than overwrite the file. Use echo -n if you don't want to add a newline after the string entered by the user (e.g. if the user is entering a password, entering something that's passed to a hashing algorithm or similarly "pedantic" stuff).