You are not logged in.

#1 2024-08-25 15:30:51

Mezzy12
Member
Registered: 2022-01-14
Posts: 43

I want a script to use my login shell: Should I still use shebang?

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 smile

Offline

#2 2024-08-25 15:40:51

Scimmia
Fellow
Registered: 2012-09-01
Posts: 12,315

Re: I want a script to use my login shell: Should I still use shebang?

To run in your current shell, you source the script instead of running it, so the shebang doesn't matter.

Offline

#3 2024-08-25 15:56:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: I want a script to use my login shell: Should I still use shebang?

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

#4 2024-08-25 18:22:42

Scimmia
Fellow
Registered: 2012-09-01
Posts: 12,315

Re: I want a script to use my login shell: Should I still use shebang?

/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)

Offline

#5 2024-08-25 19:06:02

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,323

Re: I want a script to use my login shell: Should I still use shebang?

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

Online

#6 2024-08-25 19:50:51

seth
Member
Registered: 2012-09-03
Posts: 60,787

Re: I want a script to use my login shell: Should I still use shebang?

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

#7 2024-08-25 20:00:58

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,323

Re: I want a script to use my login shell: Should I still use shebang?

Ah, now I remember

man bash wrote:

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

Online

#8 2024-08-26 08:24:52

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,732
Website

Re: I want a script to use my login shell: Should I still use shebang?

Mezzy12 wrote:

export environment variables

I use /etc/environment for that so I don't have to clutter my desktop scripts with boilerplate smile


Para todos todo, para nosotros nada

Offline

#9 2024-08-26 20:31:06

Mezzy12
Member
Registered: 2022-01-14
Posts: 43

Re: I want a script to use my login shell: Should I still use shebang?

Head_on_a_Stick wrote:

I use /etc/environment for that so I don't have to clutter my desktop scripts with boilerplate smile

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.

seth wrote:

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 smile

Offline

#10 2024-08-26 20:42:04

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,732
Website

Re: I want a script to use my login shell: Should I still use shebang?

Mezzy12 wrote:

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

#11 2024-08-26 20:51:55

Mezzy12
Member
Registered: 2022-01-14
Posts: 43

Re: I want a script to use my login shell: Should I still use shebang?

Head_on_a_Stick wrote:
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. big_smile

Offline

Board footer

Powered by FluxBB