/var/lib/FILE root:root 0600 (One line of text)

How can I read the /var/lib/FILE into $WID

 echo "$PASSWORD" | sudo -S WID=$(cat /var/lib/FILE) not working for me.
  • You may want to check the editing help and especially the first part about code blocks and inline code formatting – ilkkachu Aug 19 '18 at 12:40

If you need the file's contents in a variable in your current shell, above won't work as the assignment is done in a subshell and be lost after quitting the subshell (admittedly there are shells that do not lose the variable).

For sudo to work, you need to have an entry in sudoers allowing you to run a command. A variable assignment is not a command, thus above will fail again.

If you want a file's contents in a variable without having the permission to read that file, try

WID=$(echo "a" | sudo -S cat /var/lib/FILE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.