0

I am trying to use a bash command using another user.

sudo su - user -c ' bash -c "echo $SHELL" '

Output: /usr/bin/ksh

Does someone know what's happening? Thank you for your attention!

Antonio Costa
  • 101
  • 1
  • 6
  • Why do you think that output is wrong? – Charles Duffy Nov 03 '22 at 15:24
  • Remember, anything in double quotes is expanded early. `$SHELL` is changed to `/usr/bin/ksh` _before_ bash starts; and inspecting `$SHELL` is not the right way to check which shell is actively running anyhow in the first place (if that is in fact what you intend to do). – Charles Duffy Nov 03 '22 at 15:24
  • This command is far more complicated than it needs to be. `sudo -u user 'echo $SHELL'` would probably suffice. – chepner Nov 03 '22 at 15:25
  • @chepner, need an `-i` or somesuch in there so we aren't looking for `/usr/bin/'echo $SHELL'` or a similarly-named executable elsewhere in the PATH. – Charles Duffy Nov 03 '22 at 15:26
  • @CharlesDuffy I need to execute the command using bash. The ksh shell not woks with this command. – Antonio Costa Nov 03 '22 at 15:28
  • @AntonioCosta, yes, and `bash -c 'anything'` _does_ execute with bash. `echo $SHELL` is misleading you, for the reasons I describe above; even though it says ksh, it's actually bash that's echoing ksh. – Charles Duffy Nov 03 '22 at 15:29
  • @AntonioCosta, ...if you show a real command that doesn't work unless you're using bash, then maybe I can provide more concrete advice. – Charles Duffy Nov 03 '22 at 15:30
  • I think I can show the problem. If I do this ```sudo su - user bash echo $SHELL``` the output is this /usr/bin/ksh – Antonio Costa Nov 03 '22 at 15:36
  • Yes, and I already told you `echo $SHELL` is useless. Stop pretending it means anything. **`echo $SHELL` does not tell you which shell is in use.** And it _especially_ doesn't tell you anything useful when you don't quote correctly to cause it to be interpreted by the intended shell. – Charles Duffy Nov 03 '22 at 15:43
  • Compare to `sudo -u user bash -c 'echo $BASH_VERSION'`, which tells you the version of bash in use (and will be empty for ksh). Notice how it's important to use single quotes so `$BASH_VERSION` isn't expanded until after `bash` was started. – Charles Duffy Nov 03 '22 at 15:44
  • Quoting from https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, which is where `SHELL` is specified: *This variable shall represent a pathname of the user's preferred command language interpreter*. `SHELL` does not tell you which shell is in use; it only tells you which shell is preferred by the user. Two completely different things. – Charles Duffy Nov 03 '22 at 15:46
  • @CharlesDuffy now I got. Answering your previous question, this is what happening: When I run `bash atl.sh` works perfectly, but when I just run `atl.sh` it don't. To run without bash, I need to run `/opt/atlante/bin/atl.sh` – Antonio Costa Nov 03 '22 at 17:38
  • This is why I thought the problem was about the bash – Antonio Costa Nov 03 '22 at 17:42
  • If you run `/opt/atlante/bin/atl.sh`, then the first line is used to decide which shell to use. Make sure it contains `#!/usr/bin/env bash` or similar; if it's `#!/bin/sh` then you aren't guaranteed bash-only features. – Charles Duffy Nov 03 '22 at 18:06
  • Got it. I checked the script, and it contains `#!/bin/bash -e`. Is this sufficient? – Antonio Costa Nov 03 '22 at 19:24

0 Answers0