0

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
  • The strange placement of the quotes in the error message suggests that the .bash_aliases file has DOS-style line endings - check with file ~/.bash_aliases Aug 11, 2021 at 0:46
  • @steeldriver - comes back as .bash_aliases: ASCII text, with CRLF line terminators - I should update my question, re: locally running Windows with WSL and editing the file with the MobaTextEditor. What's odd, is I edited .bashrc with the MobaTextEditor and the file command returned .bashrc: ASCII text Aug 11, 2021 at 0:57
  • Was .bash_aliases a new file? It's possible that MobaTextEditor preserves the line terminations of existing files (like your .bashrc) but defaults to CRLF for new files. Aug 11, 2021 at 1:00
  • @steeldriver - thanks! I updated the MobaTextEditor to the correct encoding and format; reloaded everything and the aliases work out of the bash_aliases file now. If you want to make an answer, I'll accept it. Aug 11, 2021 at 1:04
  • Thanks - see below Aug 11, 2021 at 1:14

1 Answer 1

3

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
  • Very helpful. Thanks for the dos2unix tip as well. Aug 11, 2021 at 12:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .