10

I am trying to do something like

alias ftp='echo do not use ftp. Use sftp instead.'

just so that ftp will not be accidentally used. But I noticed that

ftp abcd.com

will cause the command to echo

do not use ftp. Use sftp instead. abcd.com

because the abcd.com is taken to be an argument for echo.

Is there a way to make Bash not add abcd.com to the substitution, or make echo not take it as extra arguments? (Is there a solution for each approach?)

I supposed I could make it

alias ftp='sftp'

but I just want to make the command stop all together to remind myself not to use ftp.

5
  • 3
    Mostly you can prohibit ftp in different way, but if you'd like to do so - use printf instead of echo : printf "Do not use ftp. Use sftp instead.\n\c" – Costas Nov 26 '14 at 11:00
  • Add the echo to the sftp alias? i.e. alias ftp='echo "Do not use ftp"; sftp'. You'll get the reminder, while also being forced to use sftp. – Josh Jolly Nov 26 '14 at 11:05
  • @JoshJolly I wanted to make the command to stop all together instead of letting it to "sail through" just so that I will be constantly reminded of not to use ftp – nonopolarity Nov 26 '14 at 11:14
  • 1
    Note that aliases are extremely simple; they are nothing more than simple string substitution (with the additional limitations that (1) the substitution only happens at the beginning of the command string, and (2) it is non-recursive). There is no way to "configure" how an alias works beyond the definition of the alias itself. – Kyle Strand Nov 26 '14 at 22:25
  • Er...sorry, aliases are recursive. Not sure what I was thinking. – Kyle Strand Dec 3 '14 at 0:30
18

When you define an alias, the command you set is run instead of the one you wrote. This means that when you run ftp abc.com, what is actually executed is

echo do not use ftp. Use sftp instead abc.com

A simple solution is to use a function instead of an alias:

ftp(){ echo 'do not use ftp. Use sftp instead'; }

Alternatively, you could use printf as suggested by Costas:

alias ftp="printf 'do not use ftp. Use sftp instead\n'"
16
alias ftp='echo do not use ftp. Use sftp instead. # '
1
  • 7
    +1. Yet somehow this reminds me of SQL injections :') Sweet trick. – John WH Smith Nov 26 '14 at 12:22
10

Just put a space and the comment character # at the end of the alias string:

alias ftp='echo do not use ftp. Use sftp instead. #'
ftp abcd.com
do not use ftp. Use sftp instead.

This will cause the parameters to be treated as a comment. Just do not forget to add a space before # otherwise it will not be interpreted as a separate token after the alias expansion.

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.