Score:0

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

cn flag

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.

hr flag
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`
cn flag
@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`
hr flag
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.
cn flag
@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.
hr flag
Thanks - see below
Score:2
hr flag

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.

cn flag
Very helpful. Thanks for the `dos2unix` tip as well.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.