shell issue: alias doesn't expand - windows

I have the following problem:
I have many test scripts that are currently running using MKS Toolkit. We managed to run those scripts using the mingw shell (msys) but there are other scripts that make use of some aliases that are built-in MKS Toolkit and not in bash.
The problem is that nobody wants to change those scripts, not even automatically by using a script. This means that I should define and use those aliases. I tried defining the aliases in the "/etc/profile" file of msys shell but without any success. I also tried to use shopt -s expand_aliases (in "/etc/profile") but that doesn't work either.
Could someone share an idea on how this could be done. Mind that the existing scripts will have to remain the same.
Any thoughts or ideas are welcome.
Thanks

The /etc/profile is only sourced for login shells. If you want the aliases in your script, you should put them in a separate file and source them into the script(s) that need them. For example:
aliases.sh:
alias walrus="echo coo coo cah joo"
script.sh:
#!/bin/bash
shopt -s expand_aliases
. aliases.sh
walrus
and then
$ ./script.sh
coo coo cah joo
That's probably the most reasonable way to do it. If you insist on not changing the scripts at all, then you might be able to get away with executing them indirectly like:
bash --rcfile aliases.sh -i script.sh
That will tell bash to execute an interactive shell (and thus expand aliases and source our aliases script before executing script.sh).

This answer gave me the solution. It involves defining the variable BASH_ENV before executing
bash script_name.sh
BASH_ENV will point to a script which sets the environment of the shell. In my case, to export the aliases and also the functions needed the script looks like this:
shopt -s expand_aliases
alias my_alias="echo my_alias"
function MyFunction {
echo "MyFunction(DUMB):" $*
}
export -f MyFunction

Related

is is possible to use an alias in a shellscript [duplicate]

In ~/.bashrc, I defined some aliases. But I cannot use them in other shell scripts, where I can only use aliases defined right there. Even though I sourced bashrc, it still did not work. What should I do?
PS. I'm in bash.
You need to do shopt -s expand_aliases in the script in addition to sourcing ~/.bashrc.
The simplest answer is to do the 2 important things or it wont' work.
#!/bin/bash -i
# Expand aliases defined in the shell ~/.bashrc
shopt -s expand_aliases
After this, your aliases that you have defined in ~/.bashrc they will be available in your shell script (giga.sh or any.sh) and to any function or child shell within such script.
If you don't do that, you'll get an error:
your_cool_alias: command not found
.bashrc is meant for one purpose: to configure the environment for your interactive shells. If you have code that you want shared between your .bashrc and other scripts, then it belongs in a separate file that is sourced by each of your .bashrc and shell script.
I had this problem and I reloaded the file with this command to fix it.
$ source ~/.bashrc
Stolen from enzotib on ask ubuntu: Alias are deprecated in favor of shell functions. From bash manual page:
For almost every purpose, aliases are superseded by shell functions.
To create a function, and export it to subshells, put the following in your ~/.bashrc:
petsc() {
~/petsc-3.2-p6/petsc-arch/bin/mpiexec "$#"
}
export -f petsc
Then you can freely call your command from your scripts.
There is a way of doing it globally without adding lines to each script you execute: by using the BASH_ENV variable.
Here is my setup for OS X 10.8.5:
/etc/launchd.conf:
setenv BASH_ENV /Users/DK/.env
~/.bash_profile:
# == Bash setup for interactive shells ==
# === Environment for all shells ===
. $BASH_ENV
# [skipped]
~/.env:
# == Setup for all shells ==
# This is executed for all interactive and for non-interactive shells (e.g. scripts)
shopt -s expand_aliases extglob xpg_echo
# [skipped] Misc. variables and PATH setup
# === General aliases ===
alias pause='echo "Press [Return] or [Enter] to continue..."; read' # read -p does not display prompt in Eclipse console
# [skipped]

Call bash function using vim external command

I use vim's :! external command function all the time, usually providing % as an argument to the shell command. For example :
:!psql -f %
I also have a lot of bash shell functions in my .bashrc that I use. For example:
psql-h1 ()
{
/usr/bin/psql -hh1 -d mydb "$#"
}
These bash functions aren't available from :! inside of vim. Is there a way to make them available?
Export your functions. That is:
psql-h1() { /usr/bin/psql -hh1 -d mydb "$#"; }
export -f psql-h1 ### <-- THIS RIGHT HERE
This will make them available to any copy of bash run as a child process, even if it's a noninteractive shell and so doesn't read .bashrc.
An alternative to exporting your functions (which may no reach Vim is there's a non-Bash shell in between; see here for such a case), you can instruct Vim to start an interactive shell, so that your .bashrc is read. Just pass the -i flag to Bash, via Vim's :help 'shellcmdflag'.
:set shcf=-ic
This answer assumes your vim isn't actually using bash to invoke the remote commands - this can be tested by running :!echo $0 in vim.
Specifically for vim, add:
set shell=/bin/bash
to your .vimrc.
In general, there's two strategies I've found to sometimes work when trying to get other programs to invoke my preferred shell:
export SHELL=/bin/bash
in eg. the .profile file, or:
ln -fsn /bin/bash /bin/sh
which updates the sh symlink to point to bash instead.
On many systems (certainly Ubuntu), /bin/sh is a symlink to a sh-compatible shell (such as bash), rather than the sh shell itself. Given this fact, some programs (I've seen this behaviour in GHC) seem to invoke /bin/sh, so changing where it points to will cause the programs to use bash instead.
The accepted answer didn't work for me. I'm going to go with setting shcf, as suggested elsewhere:
:set shcf=-ic
but another solutions is
!source ~/.bashrc && psql ...
Unfortunately, no solution allows the auto-completion for the command I'm creating to work properly. (The auto_completions suggested are for names of files in my current directory, rather than the ones I specified as follows in .bashrc
complete -F _generate_foo_completions foo

How do I make aliases work across all the shells?

I have been trying to make aliases work in bash shell. Now, let's say I do
alias yum='yum -v' in my shell. It works when I run the run the command from CLI. But when I run a script it doesn't have any effect. How can I make the aliases work across the shell?
In bash, you can export functions, so if you do:
yum() { command yum -v "$#"; }
export -f yum
Then the 'alias' for yum will persist in subshells. Note that functions are almost always preferred to aliases, and (from the bash man page) "For almost every purpose, aliases are superseded by shell functions.". I believe the initial clause 'For almost every purpose' is merely hyberbole and that 'almost' can be safely omitted.
From the bash manpage:
Aliases are not expanded when the shell is not interactive, unless the
expand_aliases shell option is set using shopt (see the description of
shopt under SHELL BUILTIN COMMANDS below).
As a result, you need to do the following inside your script.:
shopt -s expand_aliases
You will also need to source your file with aliases (.bashrc,.bash_profile,.bash_login etc).
shopt -s expand_aliases
source /path/to/alias/file
Please note that using alias inside your shell script will make it less portable as it will break when ran on environment that do not have your aliases.

Source my .zshrc in a bash script

Lets say I have this bash script (test):
#!/usr/bin/env bash
source ~/.zshrc
In my .zshrc, I have the following:
autoload -U compinit
compinit
When I try and run 'bash test' from my terminal window (zsh), I get errors saying autoload and compinit commands are not found. If I just do source ~/.zshrc from the command line, it works fine.
I am trying to setup my development environment, similar to this blog, but when the scripts try and source the .zshrc file it fails.
Any insight would be appreciated.
In your script, you're using bash to run a zsh script. You might as well ask the python interpreter to parse perl.
Either change bash to zsh in the shebang line or write the script with bash commands.
It's not quite as bad as python vs. perl. Both bash and zsh are derived from the Bourne shell (whose behavior is standardized by POSIX), so any script designed to work with /bin/sh is likely to work with either bash or zsh.
Normally your ~/.zshrc, as the name implies, is designed to be used with zsh, and will typically include zsh-specific commands like autoload and compinit.
You can make those commands conditional, for example:
if [ "$ZSH_VERSION" ] ; then
autoload -U compinit
compinit
fi
But of course that means you won't get the functionality of those commands, unless you can figure out a way to emulate them in bash. (I'm not familiar with either command, so I can't help you there.)
(Note that this will fail if you've done set -u or set -o nounset in your bash shell.)
But if you're going to be using both zsh and bash, it probably makes a lot more sense to have separate ~/.bashrc and ~/.zshrc files, and use each one only with the shell for which it's designed. If you want to avoid duplication, each one can source a third file containing common commands.
(And based on the comments, it's likely you're doing the wrong thing in the first place.)

Why aliases in a non-interactive Bash shell do not work

I am trying to use aliases in a non-interactive bash shell. I have defined my aliases in ~/.bashrc and I have set the variable BASH_ENV=~/startUpFile. The contents of the startUpFile are source ~/.bashrc.
I can see that my aliases are recognized, when I execute the alias command. However, if I try to use an alias defined in ~/.bashrc, Bash can't recognized it. It gives me the unknown command error.
With the TCSH shell it is pretty easy to do this because the ~/.cshrc file is always read.
Any ideas how I can do this with a Bash shell?
The command shopt -s expand_aliases will allow alias expansion in non-interactive shells.
.bashrc is only processed by interactive shells.
In addition, aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt. Unless, of course, POSIX mode is invoked by calling the shell with the name sh instead of bash.
People who use aliases a lot often source their .bashrc at the end of their profile so that the aliases are there even for non-interactive shells. This might not be the best way, but it is pretty common.
It's things like this that lead me to believe that in the 21st century we should abandon shell scripts in favor of a full-blown language like Python. It's a lot more predictable.
You have to
shopt -s expand_aliases
in the file pointed to in your BASH_ENV
I had similar issue, in the end, I found out that ~/.bashrc was all I needed.
However, in Ubuntu, I had to comment the line that stops processing ~/.bashrc :
If not running interactively, don't do anything
[ -z "$PS1" ] && return

Resources