1

I want to create an alias for curl which automatically writes the file it curls into a file with the same name in the current directory.

For example:

% curl https://bootstrap.pypa.io/get-pip.py
    curl https://bootstrap.pypa.io/get-pip.py > get-pip.py

Is there a way to make the alias read the following argument and insert it into the actual executed script like this?

3

Aliases don't handle arguments. Fortunately, functions do:

mycurl () {
    curl "$1" > "${1##*/}"
}

Where ${1##*/} is a form of Parameter Expansion which says "Remove everything to the last slash in $1".

2

If you needed to do this, I'd do it with a bash function or (in even more complicated circumstances) in its own script file in ~/bin/...

But you could just use wget instead of curl. It should do what you're asking by default and as an added bonus, you'll get to sidestep the issue that is a URL without a specified file (eg: http://example.com/).

0

There are some workarounds, where you can use combination of environment variables (PWD), command substitutions or IO redirections inside the aliases. Here is some closest example (in Bash):

alias curl='xargs -I% curl -o "%" "%" <<<'

which will save file with the same name as the URL:

$ curl https://bootstrap.pypa.io/get-pip.py
Warning: Failed to create the file https://bootstrap.pypa.io/get-pip.py

This can be further corrected by converting that URL into filename by manipulating string using shell operations, command substitution, basename, awk or some other tools.

Alternatively you can always point your curl to wget:

$ alias curl=wget
$ curl https://bootstrap.pypa.io/get-pip.py
'get-pip.py' saved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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