Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I deal with lots of servers that often have files and folders which are not under version control. This means I can just git remote add if I need to work with the files on my local machine.

With that in mind I often am browsing the server when I notice that I need a copy of a certain folder so I wrote this simple bash command to print the command so I can just copy it and run it on my local machine.

#!/bin/bash
echo "sudo rsync -av -e ssh $(whoami)@$HOSTNAME:$(pwd) ./" 

Which outputs a command like this which I can run on my home computer:

sudo rsync -av -e ssh user@hostname:/opt/lib/folder ./

Now my question is, how do I make this into a bash alias so I can add it to my user account on each of the servers?

share|improve this question
add comment

2 Answers

You can use a function instead of an alias. Just put it into your ~/.bashrc file like this:

print_rsync ()
{
    echo "sudo rsync -av -e ssh $(whoami)@$HOSTNAME:$(pwd) ./"
}

Functions are much more flexible than aliases because you can execute an arbitrary number of commands and even pass parameters.

share|improve this answer
add comment

From my understanding of shell aliases, they are static and you cannot put dynamic things like variables or commands in them.

Just copy your script somewhere in your PATH like /usr/local/bin, or create a bin folder for your user:

mkdir ~/bin
echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc
. .bashrc # or reopen terminal
cp script.sh ~/bin
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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