Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running command as login shell without starting a new shell?

Tags:

shell

zsh

I'm trying to see what the output of a command would be if I were in a login shell, without having to go into a login shell. I've tried several variations of

zsh --login -c "alias"

But none of my aliases get shown; are --login and -c incompatible?

like image 219
Marcus Buffett Avatar asked Dec 04 '25 13:12

Marcus Buffett


1 Answers

To test the difference between zsh --login -c "alias" and a normal login shell, you can/should add the -x option to see what the shell is up to.

When I run zsh -x --login -c "alias", then it processes /etc/zprofile.

When I run zsh -x --login, then it processes /etc/zprofile and /etc/zshrc.

I don't normally use zsh, so I don't have any personalized profile or start up file for it, but it seems plausible that it might look for (but, in my case, not find) ~/.zprofile and ~/.zshrc too.

I created trivial versions of those files:

$ echo "echo in .zprofile" > ~/.zprofile
$ echo "echo in .zshrc" > ~/.zshrc

and sure enough, they're processed. Further, the -c command with --login processed the .zprofile but did not process the .zshrc file.

Thus, using -c "alias" after the --login suppresses the processing of /etc/zshrc and ~/.zshrc. If you want those executed even so, you need to use something like:

zsh --login -c "[ -f /etc/zshrc ] && . /etc/zshrc; [ -f ~/.zshrc ] && . ~/.zshrc; alias"

Using -x to debug login processing is often informative.

It's nice that modern shells provide a command line option to induce login processing. I still have a program (which I don't use any more) that runs a login shell the old-fashioned way, by adding a - before the shell name in argv[0]. Thus, running -ksh would trigger login processing; the login program would run the login shell with the - at the start.

like image 137
Jonathan Leffler Avatar answered Dec 07 '25 10:12

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!