You are not logged in.
Hello.
I am typing up a small script to export environment variables and launch the sway window manager. I want it to run it like I would straight from the command line, using my login shell (zsh), so: Should I still use a shebang at the start of this script? I haven't been able to find a good answer to this online.
That's all, thanks
Offline
To run in your current shell, you source the script instead of running it, so the shebang doesn't matter.
Online
Every script needs a shebang to even be executable on it's own. So other than sourcing it as noted above, yes, it would need a shebang.
Then the question is does the script itself require zsh? Does it use any zsh-specific or even any non-posix features. If it does, then specify /bin/zsh in the shebang. If it doesn't, and you want it to run on your users default shell (even if / when you change your default shell) then use as the shebang /bin/sh as /bin/sh is a link to your default shell. (edit: yeah this was dumb. I relink /bin/sh, but every user can have a different user shell, so clearly that'd not be in /bin).
But I'm pretty sure you'll want /bin/zsh as the shebang.
Last edited by Trilby (2024-08-25 18:59:08)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
/bin/sh is not a symlink to the user's default shell. I don't know of any reasonable way of using a shebang to get the user's default shell, but I don't see how that would be useful anyway.
Last edited by Scimmia (2024-08-25 18:25:44)
Online
Does not sourcing a file use the current environment and won't changes to the environment by the script be persistent?
I agree with Trilby that one needs to identify the shell with which it is compatible. I agree with Scimmia that /bin/sh is not a link to the user's login shell
ewaller@odin/~% which sh
/usr/bin/sh
ewaller@odin/~% ls -l /usr/bin/bash
-rwxr-xr-x 1 root root 1108856 Aug 1 07:23 /usr/bin/bash*
ewaller@odin/~%
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
I want it to run it like I would straight from the command line
That means to source the function, there's no other way. It'll also pollute the current shell the same was as manual invocation would.
However, and regardless of all /bin/sh-enanigans*, you're
1. supposed to start a script w/ a shebang to invoke the desired interpreter - /bin/sh will typically serve as fallback, but that's really bad style and idea
2. clearly presenting an xy-problem: *why* do you want to "run it like I would straight from the command line"? What actual problem are you trying to achieve here?
---
Not only does /bin/sh not link your login shell, it's typically a symlink to bash but doesn't have to be and if bash is invoked as "sh" it'll run in POSIX mode (still handling a bunch of bashsms) ie.
#!/bin/sh # sh links bash
and
#!/bin/bash
are not equivalent.
Offline
Ah, now I remember
If bash is invoked with the name sh, it tries to mimic the startup be‐
havior of historical versions of sh as closely as possible, while con‐
forming to the POSIX standard as well. When invoked as an interactive
login shell, or a non-interactive shell with the --login option, it
first attempts to read and execute commands from /etc/profile and
~/.profile, in that order. The --noprofile option may be used to in‐
hibit this behavior. When invoked as an interactive shell with the name
sh, bash looks for the variable ENV, expands its value if it is defined,
and uses the expanded value as the name of a file to read and execute.
Since a shell invoked as sh does not attempt to read and execute com‐
mands from any other startup files, the --rcfile option has no effect.
A non-interactive shell invoked with the name sh does not attempt to
read any other startup files. When invoked as sh, bash enters posix
mode after the startup files are read.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
export environment variables
I use /etc/environment for that so I don't have to clutter my desktop scripts with boilerplate
Para todos todo, para nosotros nada
Offline
I use /etc/environment for that so I don't have to clutter my desktop scripts with boilerplate
I'm trying to set up the VDPAU_DRIVER=radeonsi environment variable and as per https://wiki.archlinux.org/title/Enviro … nvironment, as it's to do with graphics. The article suggests one of those methods to restrict the scope of environment variables but idk how much that really matters.
2. clearly presenting an xy-problem: *why* do you want to "run it like I would straight from the command line"? What actual problem are you trying to achieve here?
I have it in my head that I wanted a portable script that uses whatever login shell you're currently using. That's probably very silly considering it's just launching sway with some environment variables, but I was just wondering if there's any good way to do it
Offline
restrict the scope of environment variables
Would it be bad for your system if those variables were exposed outside the graphical session?
FWIW I export VDPAU_DRIVER in /etc/environment and I've never noticed any ill effects in the console, which I do use occasionally.
Para todos todo, para nosotros nada
Offline
Mezzy12 wrote:restrict the scope of environment variables
Would it be bad for your system if those variables were exposed outside the graphical session?
Not that I can see, no. It's just that the Arch Wiki page I linked in my last post suggested it might be a good idea, so I said to myself, huh, might as well try it.
Offline