I Tried to run the following similar commands in CentOS (12.x):

printf "\nhello" "$HOME"/.bashrc
printf "\nhello" ${HOME}/.bashrc

After execution I display the content of the .bashrc file and see nothing new.

set -x brings an empty line of output for both so my command is likely wrong.

I had a similar problem with echo syntax (edit: I'm likely to misrecall but I recalled I could append content with echo without appendation operator like >>).

What you see wrong?

  • 1
    You are missing a redirection operator... – jasonwryan Jul 11 at 20:58
  • I swear I remembered in the past I used echo without it and still appended content. – user9303970 Jul 11 at 21:01
  • 2
    @user9303970 Using echo without redirection would not append text to any file, unless the echo was part of a composite command that was redirected as a whole, or a redirected script. – Kusalananda Jul 11 at 21:04
up vote 3 down vote accepted

If you are trying to redirect the output to your .bashrc file you need a redirection operator > (will overwrite the file) or in your case >> (will append the file) is probably more appropriate.

printf '\n%s\n' "hello" >> "${HOME}/.bashrc"

A Kusalananda points out it's generally a good idea to put a trailing newline at the end of your file. Some tools expect a file to end with a newline and could behave unexpectedly if it's not there.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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