Alias fails when defined in .bash_aliases on Ubuntu 20.04.2 over ssh with jump server

Spun up an AWS Ubuntu 20.04.2 server. This server sits behind a jump server.

Once the server spun up, I ran:

sudo update
sudo upgrade

As well as rebooted the server. After that, created an ~/.bash_aliases file and added the following alias:

alias lsd='ls -a -g --group-directories-first'

After saving the .bash_aliases, reloaded .bashrc: source ~/.bashrc

When using the alias, the following error is shown:

xxx@ip-1-2-3-4:~$ lsd
's: unrecognized option '--group-directories-first
Try 'ls --help' for more information.

Verified the following:

  • full command will run without the alias
  • alias works when adding directly to the ~/.bashrc file

Also verified .bashrc is reading in the aliases:

if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases
fi

Question: How can I use the .bash_aliases file over ssh?

I've read through a similar question, but the solution there doesn't seem to work: Why doesn't my alias work over ssh?

I'm coming from a Windows background and just now starting to work in the Linux world; be gentle.

Edit for Context

The .bash_aliases file was edited locally on Windows with the MobaTextEditor while ssh'd into the server. Even though the empty file was created on the server, the Unix style encoding was not implied in the editor as the file was empty. Windows encoding was added to the file after editing and saving back to the server with the MobaTextEditor.

5

1 Answer

The strange placement of the quotes in the error message suggests that the .bash_aliases file has DOS-style CRLF (i.e. \r\n) line endings ex.

$ printf '%s\r\n' "alias lsd='ls -a -g --group-directories-first'" >> ~/.bash_aliases
$ file ~/.bash_aliases
/home/steeldriver/.bash_aliases: ASCII text, with CRLF line terminators
$ . ~/.bash_aliases
$
$ lsd
's: unrecognized option '--group-directories-first
Try 'ls --help' for more information.

You can fix it by setting appropriate UNIX line termination in your text editor, or by passing the file through dos2unix:

$ dos2unix ~/.bash_aliases
dos2unix: converting file /home/steeldriver/.bash_aliases to Unix format...

You will then need to source the file again.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like