1

I am using Windows 10. I followed this tutorial on how to create bash aliases. I put the aliases in ~/.bashrc, and used source ~/.bashrc afterwards. When I tried to use one of the aliases, I get the following error:

$ docs
bash: docs: command not found

I opened a new PowerShell session, used bash, tried the alias and it still gave the same error. But it only worked when I tried it on Git Bash. From what I've researched, I was opening bash in a non-interactive shell. I tried the answer on this post and put shopt -s expand_aliases at the end of .bashrc, but it still wouldn't work. This is what my .bashrc looks like:

######## Aliases #######

# Git
alias gcl="git clone"
alias gco="git commit"
alias gcom="git commit -m"
alias gpus="git push"
alias gpul="git pull"
alias gbr="git branch"
alias gad="git add"
alias gre="git restore"
alias grest="git restore"
alias gsw="git switch"
alias gst="git status"
alias gcoam="git commit --amend"
alias gcoamne="git commit --amend --no-edit"
alias gcoamm="git commit --amend -m"

# Navigation
alias home="cd ~"
alias docs="cd ~/Documents"
alias labs="cd ~/Documents/VSCode"

alias open="code ."

################

shopt -s expand_aliases # allows aliases available in non-interactive shells

How do I make the bash aliases work in a non-interactive shell?

yees_7
  • 114
  • 8
  • try `~/.bash_profile` – Mor Blau Jul 05 '22 at 04:51
  • @MorBlau : Since the OP explicitly sourced .bashrc, it is pointless to put the aliases into a different file. – user1934428 Jul 05 '22 at 05:32
  • @MorBlau I put all the `.bashrc` code into `.bash_profile`, so now `.bashrc` is empty. Then I used `source` on `.bashrc` and `.bash_profile`. The aliases still wouldn't work. What do i do? – yees_7 Jul 05 '22 at 05:34
  • Of course they don't. See my answer to understand the issue. – user1934428 Jul 05 '22 at 05:35
  • BTW, I'm confused as how you create your non-interactive shell. If you enter a plain `bash` on the command line, you get of course an interactive shell. How did you verify whether your shell is interactive? The safest way is to do a `echo $-` inside the shell, which will output a bunch of characters. If one of the characters is a lower-case `i`, your shell is interactive; otherwise it is non-interactive. – user1934428 Jul 05 '22 at 08:06
  • @user1934428 I did what you said, and an `i` did come up. So I guess that I don't understand what interactive and non-interactive shells actually are. – yees_7 Jul 05 '22 at 09:39
  • An interactive shell is a shell where the stdin and stdout for the bash process is bound to a tty. Commonly this is known as command line. By and large: You get a prompt, enter a command and see the response. If you run a shell script, you would be annoyed if the script would print a prompt before executing a statement. However the absence of a prompt does not mean that we are non-interactive: If you _source_ some bash code (i.e. stay in the same process) from an interactive bash, the sourced commands still are executed in an interactive shell, although you don't get a prompt in between. – user1934428 Jul 05 '22 at 11:02
  • However, in an interactive shell, alias expansion should be turned on (unless it had been turned off explicitly). You can query the state of this option by doing a `shopt expand_aliases`. If it says **off**, we need to search where you are turning it off. – user1934428 Jul 05 '22 at 11:03

1 Answers1

0

You have to turn on this feature:

shopt -s expand_aliases
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Where do i put this? I've already tried putting it in my `.bashrc`, which was explained in the question – yees_7 Jul 05 '22 at 06:23
  • It needs to be seen by **that** process which needs to do the expansion, i.e. your script! You are talking about a **non**-intreactive bash, which means that you are running a bash script. BTW, if you read the bash man-page, section _ALIASES_, you find all of this explained. In particular it says: _Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt_ – user1934428 Jul 05 '22 at 08:00
  • Actually, I would not mix definitions for the command line and those for scripts in a single file. This will bite you sooner or later. Instead I would create a file (say: `~/etc/alias_lib`) which contains the `shopt` command, followed by your alias definitions. You then do a `. ~/etc/alias_lib` from within your .bashrc and also from within your script. – user1934428 Jul 05 '22 at 08:03
  • This works, but only when I source both `.bashrc` and the script file in a new session. Is there some shortcut to prevent this? Also, I don't know if it is related, but for some reason using `docs` or `labs` gives an error that there is no such directory called `home/joshu/Documents`. This also happens when I try to use `cd`, and when i used `dir`, nothing came up. My fix is to `cd C:` and go to other directories from there. I wonder if there is a problem with the `~`. – yees_7 Jul 05 '22 at 10:39
  • @yees_7 : What is _a new session_? Read the bash man page about shell invocation: _When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option._ If you find that you **have to** source .bashrc, it means that you are in an interactive **login** shell. You can again do a `echo $-`: A login shell has a lower case `l` here. – user1934428 Jul 05 '22 at 11:07
  • @yees_7 : Basically you need first to understand what **kind** of bash you have. From this you know which files are automatically sourced when the shell starts up, and from this you can conclue in which file you have to put your definitions. – user1934428 Jul 05 '22 at 11:08
  • I tried `echo $-` and it came up with `himBHs`, so no, this isn't a login shell. I realised that I actually didn't need to source `.bashrc` but actually the aliases file. The commands only work when I go to the parent directory of the script file, and `source aliases` from there. It gives an error, but when I typed `home`, it still worked. `source ~/etc/aliases` gives the exact same error, but the commands don't work. By the way, a _new session_ is every time I use `bash` from the command line. The aliases work perfectly when I open **Git Bash** application instead. – yees_7 Jul 06 '22 at 00:29
  • @yees_7 : Well, if you had followed my advise to source the alias-file from within .bashrc, you would not have to source it explicitly. `source ~/etc/aliases` itself won't produce any error, unless the alias-file itself has an error (in which case you have to fix this error first). According to what you have written before, only **using** the alias gives an error, so I'm a bit confused of what you problem exactly is. – user1934428 Jul 06 '22 at 06:14
  • It turns out the `home` (`~`) directory was in the wrong spot. My files were located in `C:/Users/user` while the home directory was actually in my **MSYS2** folder. [This post](https://stackoverflow.com/questions/33942924/how-to-change-home-directory-and-start-directory-on-msys2) helped me. It works, but everytime I open bash, the error `bash: /c/Users/user/bash_commands/aliases: Bad address` comes up. I will just ignore this error, but if you have an idea, please reply. – yees_7 Jul 07 '22 at 07:45
  • I would ask this in a new question. Make sure that you not only post the content of the _aliases_ file, but also the exact command to source it. In addition, I would turn on `set -x` before sourcing it (and turn it off afterwards with `set +x`), so that you see which command exactly causes the error. – user1934428 Jul 07 '22 at 08:06