Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run alias from bash_profile in OS X #40

Open
fliebe92 opened this issue May 22, 2017 · 12 comments
Open

Run alias from bash_profile in OS X #40

fliebe92 opened this issue May 22, 2017 · 12 comments

Comments

@fliebe92
Copy link

Hi,

nice work! I really like this atom package. At the moment I am trying to include this in my tool chain.

Is it possible to create a process-palette command that will run an alias from my bash_profile? I am not able to get this running right now.

Thanks for your help!

Cheers
Florian

@morassman
Copy link
Owner

Hi @Irthen

I've never tried this before, but now that I did I am also not able to get aliases to work. I'm not 100% sure why it isn't working. My hunch is that bash_profile is only loaded when one uses a terminal. process-palette uses shelljs for running commands, which in turn calls Node.js's child process. It might be that bash_profile isn't loaded then.

I searched around a bit, but wasn't able to find out what exactly is happening or how to solve it. I'll look around some more, but thus far I haven't had any success.

@fliebe92
Copy link
Author

Hi @morassman!

Thanks for your research.

Another idea by me, I don't have time to test it right now: Is it possible to do anything like "source <path/to/.bash_profile>" and after that call an alias?

Maybe this will solve the problem.

@morassman
Copy link
Owner

morassman commented May 23, 2017 via email

@fliebe92
Copy link
Author

Ah, ok - nice to know, thx. Then I will skip this. :)
Maybe I am going to duplicate my alias and give it a try in my tool chain.

Please feel free to do some research if you would like to do it - maybe you can handle it in some way. Thx!

@hg42
Copy link

hg42 commented May 24, 2017

It's all documented: man bash.

So, .bash_profile is only invoked in interactive login shells:
"
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
"

Even .bashrc is only read in an interactive (non-login) shell, but --rcfile can force reading any file.

Or you could use BASH_ENV:
"
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the filename.
"

@hg42
Copy link

hg42 commented May 24, 2017

but...
today shells (or file systems / OS) are fast enough to simply turn all aliases into scripts...that's what I do. I don't see any need for aliases. And we have functions...

@morassman
Copy link
Owner

I read the bash documentation regarding this myself, but still wasn't able to figure out how to use aliases from commands that are run from process-palette.

Whether aliases are actually needed or not, the question still stands whether it's possible to use them from commands run from process-palette. I don't consider redefining the aliases as part of a script that is executed as a solution. Ideally one wants to define them in any of the files you listed and be able to use them without needing to redefine them. The question is more how one can get those files to be taken into account when running commands from process-palette.

@hg42
Copy link

hg42 commented May 24, 2017

I use zsh, which has several files for different situations e.g. .zshenv, .zshrc, .zlogin, .zlogout and you have different possibilities to configure this (simple or fine grained and compatible modes).
Usually aliases are only used interactively, so they are not designed to be executed in scripts or from applications.
Because of this aliases are defined in .bash_profile.

@hg42
Copy link

hg42 commented May 24, 2017

.bashenv should be executed in all shel if BASH_ENV is set to ~/.bashenv

I read that some systems have this in /etc/environment

Alternatively, you could set it yourself in a login shell (some *profile file).

@hg42
Copy link

hg42 commented May 24, 2017

Ok, I'm not a bash expert (using zsh for min. 10 years) and I often wonder about bash problems.

So, I tested it...
I created a .bash_profile:

echo executing .bash_profile
alias alias_test="echo bash alias test"

From a zsh shell window I get this:

% bash -c "echo test"
test

% bash -l -c "echo test"
executing .bash_profile
test

% bash -l -c "alias_test foo bar"
executing .bash_profile
bash: alias_test: command not found
# wow, why not? looks like aliases are only added in interactive shells.

% bash -i -l -c "alias_test foo bar"
executing .bash_profile
bash alias test foo bar
# ok, with `-i` it works

Now, man bash describes this behavior:
"
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).
"
so adding shopt -s expand_aliases helps:

shopt -s expand_aliases
echo executing .bash_profile
alias alias_test="echo bash alias test"

now I get:

% bash -l -c "alias_test foo bar"
executing .bash_profile
bash alias test foo bar

"Interesting", this doesn't work:

% bash -c 'shopt -s expand_aliases; alias alias_test="echo xxx"; alias_test foo bar'
bash: alias_test: command not found

but this:

% bash -l -c 'shopt -s expand_aliases; alias alias_test="echo xxx"; alias_test foo bar'
executing .bash_profile
bash alias test foo bar

EDIT: no, it's the alias from the .bash_profile, not the one defined on the command line, so this doesn't work either!

zsh can do this, but it's also puzzling:

% alias alias_test="echo xxx"; alias_test foo bar  
zsh: command not found: alias_test
                                                                                                                                            
% alias alias_test="echo xxx"; alias_test foo bar
xxx foo bar

seems like the alias is only defined after the rest of the line is executed.

@fliebe92
Copy link
Author

Turning my aliases to functions is not a real problem for me, but how can I use functions which are defined in my .bash_profile from process-palette? I do think that's the same problem with the aliases, isn't it?

@hg42
Copy link

hg42 commented May 26, 2017

there ate two problems:

  • your .bash_profile is not loaded without special action.
    This can be solved in several ways, e.g. use -l (use 'BASH_OPT' in prpvess-palette?) or BASH_ENV

  • aliases are only defined in interactive login shells.
    I didn't test this, but I assume this is solved by functions.
    Or you may put shopt ... in your file to make aliases work.
    Or convert them to scripts (use your own bin/ and set the path),

Now it depends on what you want.

For which purposes do you use these aliases?
And why do you want them to be used from process-palette?

With BASH_ENV you can even force loading a file on each and every bash invocation, so that functions in this file are loaded everywhere.
But is this the right thing to do?

aliases are designed for interactive use. They are kind of shortcuts in bash, e.g. you cannot use parameters.
So if you do anything that matters with aliases, it would be better to convert them to functions or better scripts.
Scripts can be invoked by everyone,
(bash) functions only work in bash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants