Skip to main content
Solved

Write Exact Text to File

  • October 24, 2023
  • 2 replies
  • 23 views

Forum|alt.badge.img+5
  • New Contributor
  • 8 replies

Hello, 

Question: How do you write the exact text to the file instead if it spelling out the $PATH ??

I am trying to copy a line with the echo command to a text file. The command works, however it writes the actual path instead of the text itself. 

Example: 

echo "export PATH=~/.npm-global/bin:$PATH" | cat >> /Users/$CURRENT_USER/.profile

Writes: export PATH=~/.npm-global/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/jamf/bin 

Instead of " export PATH=~/.npm-global/bin:$PATH " 

I am scratching my head on this and can't seem to find any answers online, running as the user does not seem to work either as Jamf still fills in the $PATH with it's own seen above. Running from terminal as user still fill's in the path instead of the exact text. 

 

Best answer by mm2270

You need to escape the $ in the $PATH variable so the shell won't do expansion.

echo "export PATH=~/.npm-global/bin:\\$PATH" | cat >> /Users/$CURRENT_USER/.profile

2 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • Answer
  • October 24, 2023

You need to escape the $ in the $PATH variable so the shell won't do expansion.

echo "export PATH=~/.npm-global/bin:\\$PATH" | cat >> /Users/$CURRENT_USER/.profile

Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 8 replies
  • October 24, 2023

You need to escape the $ in the $PATH variable so the shell won't do expansion.

echo "export PATH=~/.npm-global/bin:\\$PATH" | cat >> /Users/$CURRENT_USER/.profile

This solved the issue, thank you mm2270!