Run command as bash from POSIX shell - bash

I have a quick question
I have a posix shell but I need to run a bash command.
Think
root#home:~# sh
# /bin/bash /bin/ls
However, when I do that, I get
/bin/ls: /bin/ls: cannot execute binary file
I'm sure I'm missing something simple, but I'm not sure what it is. Any help? I also need to do it in one line.

Use the -c argument to specify a command that you want to be ran by the other shell:
" -c Read commands from the command_string operand"
bash -c "ls"

I came up with a workaround
echo '#!/bin/bash\--comamnd--; chmod ugo+x /tmp/script.sh; /tmp/script.sh

Related

Run bash command from windows Command line (WSL)

I have installed WSL on Windows 10 Pro.
And I need to execute bash commands from Windows Command Line like this:
bash -c ll
Expected: ll command output in Command Line console
In practice: /bin/bash: ll: command not found
But its work for some commands like ls or apt.
Please, see :
What could be the problem?
ll is a common alias (for ls -alF in WSL; defined in the default .bashrc). Depending on how you invoke bash will determine whether the scripts which set up your system aliases are run. See the INVOCATION section of the bash manual.
You can use bash -i -c ll to invoke bash in an appropriate way for WSL.
Apparently ll is an alias you defined in some of your configuration files. You should start bash as follows:
bash -ilc ll
Depending on where you defined the aliases, you can omit the -i or -l flag.
ll is usually an alias of 'ls -l and can't (shouldn't) be used in script or command line.
Instead use directly the command itself: bash -c 'ls -l'.
To see if a certain command is an alias use the command type:
type ll
ll is aliased to `ls -l'

read: Illegal option -s in shell scripting

I tried running this code:
#!/bin/bash
read -s "Password: " password
With command:
run sh init.sh
it throws an error: read: Illegal option -s. Any help.
I take it you're using Debian/Ubuntu, or a BSD-derivative?
When you execute a command like run sh init.sh (although I'm not myself familiar with this run command) you are overriding the #!/bin/bash shebang. In your case sh is a strictly compliant POSIX shell like dash, where, in fact, the only argument to read that is not an extension is -r.
So maybe you'd want to use run bash init.sh instead?

Is it possible to access the value associated with the "-c" parameter from a bash script?

I would like to do bash -c '<some smart bash script>' that would output the actual string passed to -c.
In other words, if SCRIPT is set to that smart script, bash -c "$SCRIPT" should yield what's in $SCRIPT.
Is that possible? If yes, how?
Thanks
See man bash
5.2 Bash Variables
...
BASH_EXECUTION_STRING
The command argument to the -c invocation option.
If you try
bash -c 'echo $BASH_EXECUTION_STRING'
it will give you
echo $BASH_EXECUTION_STRING

How to specify zeroeth argument

I'm writing a bash script that starts the tcsh interpreter as a login shell and has it execute my_command. The tcsh man page says that there are two ways to start a login shell. The first is to use /bin/tcsh -l with no other arguments. Not an option, because I need the shell to execute my_command. The second is to specify a dash (-) as the zeroeth argument.
Now the bash exec command with the -l option does exactly this, and in fact the following works perfectly:
#!/bin/bash
exec -l /bin/tcsh -c my_command
Except... I can't use exec because I need the script to come back and do some other things afterwards! So how can I specify - as the zeroeth argument to /bin/tcsh without using exec?
You can enclose the exec command into a sub-shell of your script.
#!/bin/bash
(exec -l /bin/tcsh -c my_command)
# ... whatever else you need to do after the command is done
You can write a wrapper (w.sh) script that contains:
#!/bin/bash
exec -l /bin/tcsh -c my_command
and execute w.sh in your main script.

read: Illegal option -d

Here is the offending part of my script:
read -d '' TEXT <<'EOF'
Some Multiline
text that
I would like
in
a
var
EOF
echo "$TEXT" > ~/some/file.txt
and the error:
read: 175: Illegal option -d
I use this read -d all over the place and it works fine. Not sure why its not happy now. I'm running the script on Ubuntu 10.10
Fixes? Workarounds?
If you run sh and then try that command, you get:
read: 1: Illegal option -d
If you do it while still in bash, it works fine.
I therefore deduce that your script is not running under bash.
Make sure that your script begins with the line:
#!/usr/bin/env bash
(or equivalent) so that the correct shell is running the script.
Alternatively, if you cannot do that (because the script is not a bash one), just be aware that -d is a bash feature and may not be available in other shells. In that case, you will need to find another way.
The -d option to read is a feature unique to bash, not part of the POSIX standard (which only specifies -r and -p options to read). When you run your script with sh on Ubuntu, it's getting run with dash, which is a POSIX shell, and not bash. If you want the script to run under bash then you should run it with bash, or give it a #!/bin/bash shebang. Otherwise, it should be expected to run under any POSIX sh.

Resources