While trying to write a script, I found an interesting issue with cat
today. If I do the following at the command line, everything works properly:
var=$(ssh user@server "cat /directory/myfile.sh")
echo $var > ~/newfile.sh
This works and I have a script file with all the proper formatting and can run it. However, if I do the EXACT same thing in a script:
#!/bin/sh
var=$(ssh user@server "cat /directory/myfile.sh")
echo $var > ~/newfile.sh
The file is mangled with carriage returns and weird formatting.
Does anyone know why this is happening? My goal is to ultimately cat a script from a server and run it locally on my machine.
EDIT
I now know that this is happening because of my invoking #!/bin/sh
in my shell script. The command line works because I'm using zsh
and it is preserving the formatting.
Is there a way to cat back the results regardless of the shell?