1

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 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

1

Create shell script with:

vi usershell.sh

Enter the script to gather user defined string:

read user_data
echo $user_data >> user_string

Execute the shell script

It will ask user to enter string and will store it in file called “user_string”.

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.