Unable to source .profile without .zshrc - unix

I have the following lines in my .zshrc
source /etc/profile
source $HOME/.profile
My .profile is not loaded without them, although .profile should be loaded by default in Unix systems.
How can you load .profile without sourcing it at .zshrc?

Look in the Zsh manual where it says to use ~/.zprofile instead of ~/.profile.

In addition to what Martin says about zprofile vs profile, also note that /etc/zprofile and ~/.zprofile are only read when zsh is started as a login shell. If it is "just" an interactive shell, it will only read .zshrc. So the question is how you are starting the zsh. If you use something like Gnome Terminal, you can set in the Profile Preferences that the shell should be started as a login shell.
More info: Z-Shell User's Guide

Related

Can I use zsh as the default non-interactive shell for WSL2 Ubuntu?

I am trying to use Run/Debug Configurations on WebStorm, however it doesn't seem to source .zshrc and produces errors about not finding commands and environment variables. (An example of this would be yarn tauri dev when using Tauri)
I have installed Ubuntu 20.04 in WSL and the project I opened in WebStorm resides under the $HOME directory. WebStorm is installed in Windows.
For the interactive shell, I have made zsh the default by chsh -s $(which zsh), but when using Run/Debug Configurations it uses the default non-interactive shell, which is dash as far as I know. And my environment variables and PATH are all set in .zshrc, which is not sourced by dash.
It seems in CLion, it is possible to execute commands in the login shell according to this YouTrack issue, but such an option is not available on WebStorm.
Is it possible to use zsh instead of dash as the default non-interactive shell? If not, it would help me a lot to know what is the best practice in such situations.
There are several questions and points you make:
First, from the question title (and the summary at the end):
Can I use zsh as the default non-interactive shell for WSL2 Ubuntu?
Well, maybe (using symlinks), but it would be a really bad idea. So many built-in scripts rely on /bin/sh pointing to Dash, or at least Bash. While Zsh might be compatible with 99.9% of them, eventually there's a strong likelihood that some difference in Zsh would cause a system-level script to fail (or at least produce results inconsistent with those from Dash).
It is possible in Ubuntu to change the default non-interactive ("system" shell) from Dash to Bash with sudo dpkg-reconfigure dash. If you select "No" in the resulting dialog, then the system will be updated to point /bin/sh to bash instead of dash.
But not to Zsh, no.
when using Run/Debug Configurations it uses the default non-interactive shell, which is dash as far as I know
I don't run WebStorm myself, so I'm not sure on this exactly. Maybe #lena's answer (or another) will cover it for you, but if it doesn't, I'm noticing this doc page. It might be worth trying to specify Zsh in those settings, but again, I can't be sure.
And my environment variables and PATH are all set in .zshrc, which is not sourced by dash.
Hmm. I'm guessing you would need these set in a .profile/.zprofile equivalent regardless. I would assume that WebStorm is executing the shell as a non-interactive one, which means that it wouldn't even parse ~/.bashrc if Bash was your default shell.
... it would help me a lot to know what is the best practice in such situations.
Best practice is probably to make sure that your ~/.profile has any environment changes needed. Yes, this violates DRY (don't repeat yourself), but it's probably the best route.
Thanks to the answer here and the discussion below, I was able to figure it out. (Thank you, #NotTheDr01ds and #lena.)
The main problem is that WebStorm is installed on Windows and therefore knows only the environment variables in Windows. There are two ways to solve the problem as follows.
Sharing WSL's environment variable to Windows through WSLENV
Add the line below to .zshrc so that it sets $WSLENV when zsh starts.
export WSLENV=VAR_I_WANT_TO_SHARE:$WSLENV
# Don't forget to insert the colon
# And for some reason, appending the variable after $WSLENV didn't work well
In Windows, run
wsl -e zsh -lic powershell.exe
This runs WSL using zsh (logged-in and interactive), then runs powershell which brings you back to Windows. Although this doesn't seem to achieve anything, by going through zsh in WSL, .zshrc was sourced and therefore $WSLENV set as well. You can check if it worked well by running the below command after you've run the above.
$env:VAR_I_WANT_TO_SHARE
Run WebStorm from the PowerShell that was just created.
& 'C:\Program Files (x86)\JetBrains\WebStorm 2022.1.3\bin\webstorm64.exe'
When you run or debug any of the Run/Debug Configurations, you will see that the environment variable is shared successfully.
Setting the PATH in Windows
For most environment variables, the previous method works well. However, PATH is an exception. The Windows PATH is shared to WSL by default. The opposite doesn't work, probably because the PATH in WSL should not interfere with Windows.I've tried adding the $PATH of WSL into $WSLENV but it didn't seem to work.
In the end, what I did was manually adding each needed $PATH of WSL into the Windows PATH.
For example, if there was export PATH=$PATH:home/(username)/.cargo/bin in .zshrc, you can then add \\wsl$\Ubuntu\home\(username)\.cargo\bin to the Windows $env:Path using the Environment Variable window.
I might have made some mistakes, so feel free to leave an edit or comments.
You can try using npm config set script-shell command to set the shell for your scripts. Like npm config set script-shell "/usr/bin/zsh".
When npm run <script name> spawns a child process, the SHELL being used depends on NPM environment. Cм https://docs.npmjs.com/cli/run-script:
The actual shell your script is run within is platform dependent. By
default, on Unix-like systems it is the /bin/sh command, on Windows it
is the cmd.exe. The actual shell referred to by /bin/sh also depends
on the system. As of npm#5.1.0 you can customize the shell with the
script-shell configuration
See also https://github.com/npm/npm-lifecycle/blob/10c0c08fc25fea3c18c7c030d4618a401963355a/index.js#L293-L304

How to automatic load .profile in Solaris 10

I have set some of my default PATH that I wanted to run in the .profile.
However the machine forget it everytime I logout and I have to reload the profile everytime by typeing source ./.profile
Is there a way to set it to startup or something similar ?
As you are using source, you are likely using bash given the fact source is not supported by /bin/ksh or /bin/sh under Solaris.
Bash is reading .bash_profile when it exists and in such case, ignores ~/.profile.
You then need to set your PATH in .bash_profile.

Why isn't my ZSH shell not changing the prompt?

I was using bash before switching to zsh.
On my ~/.zsh I have export PS1="\w ~ " but it is not making those changes to reflect the prompt. I want my prompt to show something like Documents/Rails_App/views ~
I have opened a new tab and closed re-opened the app.
The first level prompt of Zsh can be set in with either PROMPT, prompt or PS1.
Zsh uses other escapes than Bash. Most notably do they start with % not \. The equivalent to Bash's \w would be %~.
So just putting setting the following line in your ~/.zshrc should do the trick:
PROMPT='%~ ~'
Note: There is (usually) no need to export PROMPT in Zsh - or Bash for that matter.
If it does not work for you, it most likely has to do with Oh-My-Zsh as it already provides its own settings for PROMPT. So you have to make sure that you set PROMPT after Oh-My-Zsh has been sourced.
If you used the template that came with Oh-My-Zsh it should be after this line:
source $ZSH/oh-my-zsh.sh
On Debian distro.
After installing zsh from standard source apt install zsh comment #prompt adam1 inside .zshrc file and reload settings source ~/.zshrc.
Now add custom prompt: PROMPT='Greetings %n '
Image: zsh custom prompt
Use _PROMPT in zsh:
export _PROMPT='%d ~ '

Can't open vim in zsh?

I recently decided to switch from zsh to bash. However, for some reason that escapes me, I cannot open vim within zsh after I change my default shell to zsh (chsh -s /bin/zsh). Currently, my default shell is bash. If I just use zsh to enter a zsh shell, I have no problems with vim. The problems arise when I try to change my default shell, then open a new terminal window. Any insight would be great as I'd love to be able to play around with zsh.
vim probably lives in a directory that is added to your PATH environment variable in one of your bash start up files. When you start zsh manually, that PATH is inherited by zsh. When you make zsh your default shell, it inherits a PATH which is not modified to include the directory for vim. In bash, type
which vim
to find out which directory vim lives in. Let's say it /usr/other/bin/. Then you would add the following to your .zshenv file (creating it if necessary):
path+=/usr/other/bin
New zsh sessions should now be able to run vim.

How can I use oh-my-zsh with PHPStorm?

I just got oh-my-zsh (a very good command line utility) and would like to utilize this within the PHPStorm terminal. How can I accomplish this?
As oh-my-zsh is more of an extended zsh-configuration, it should be sufficient to just tell PHPStorm to use zsh as Shell. This can be done in the Settings dialog (Menu File->Settings) under section Project Settings->Terminal. Just change Shell Path to the path of zsh (can be found out with type zsh in a terminal).
To affect every (new) project you may have to repeat this in File->Default Settigs
For once and all setup:
Set it in Preferences->Tools->Terminal and Shell Path = /bin/zsh
restart your PhpStorm and thats it.

Resources