Download one or more files to your local machine via SSH

Use scp.

# Download single file
local> scp username@host.com:/path/to/dir/file local/path 

# Download directory recursively
local> scp -r username@host.com:/path/to/dir local/path 

– via Stack Overflow

Create a script that runs over SSH

Basic script

ssh <your-server> << EOF
    echo "Hello world"
    echo "This is where your script should be"
EOF

– via Amit Chaudhary

Displaying the ssh command outputs and avoiding the “Pseudo-terminal will not be allocated because stdin is not a terminal.” warning

ssh <your-server> -tt << EOF
    echo "Hello world"
    echo "This is where your script should be"
EOF

– via Stack Overflow

Bonus: Setting up a ssh key for passwordless auth

ssh-keygen -t rsa -b 2048

ssh-copy-id id@server -i <path_to_your_new_key>

ssh id@server  -i <path_to_your_new_key>

I’m including the path to the new key since I’m using a dedicated, passwordless key for this specific instance. If you’re just setting up the default key (~/.ssh/id_rsa) then you won’t need the -i option.

– via Stack Overflow

Remove double quotes characters surrounding text

Two approaches:

text="\"hello world\""

# 1. Using sed, this doesn't look nice but will work as expected and will only remove surrounding quotes
sed -e 's/^"//' -e 's/"$//' <<<"$text"

# 2. Using tr, this looks way nicer but will also remove quotes from the middle of the text
echo "$text" | tr -d '"'

# 3. Using cut which was recommended to me by Copilot and, surprisingly, it works
cut -d '"' -f2 <<<"$text"

– via Stack Overflow