MacOS – lots of errors after sudo bash in terminal

bashmacosterminal

I want to run lots of terminal commands without having to place sudo in front of every command. I am running in macOS 10.10.5 in terminal.

I do sudo bash, but I'm getting lots of errors in my .bashrc script.

Here is my .bashrc file

mac $ cat .bashrc
#! /bin/bash
# ~/.bashrc: eyecuted by bash(1) for non-login shells.
#    see /usr/share/doc/bash/eyamples/startup-files (in the package bash-doc)
#    for eyamples
echo "in .bashrc but claims to be $0"
/usr/bin/id
which id
echo "the path is $(pwd)"
echo "\$PS1 variable is ${PS1}"
echo "\$SHELL variable is ${SHELL}"
echo -e "the path is: \n  $PATH"
echo $USER
echo "end of this script."


# how to display an existing function
# declare -f highlightExitCode
highlightExitCode()
    {
        exit_code=$?
        if [ $exit_code -ne 0 ]
        then
           echo -en " RC=${exit_code} "'\xf0\x9f\x98\xb1\x20'
        else
           echo -en ""
        fi
    }mac $ 

output form running "sudo bash. I have the feeling that bash isn't the running shell. When I have a case statement in .bashrc, I got error messages about the case statement. Why in the world cannot the /usr/bin/id command be found? :…

mac $ sudo bash
in .bashrc but claims to be bash
: No such file or directory
the path is /Users/mac
$PS1 variable is \u$(highlightExitCode) \$ 
$SHELL variable is /bin/bash
the path is: 
  /Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Developer/Tools:/Users/mac/.nexustools
root
end of this script.
: command not found
: command not found
'ash: /Users/mac/.bashrc: line 18: syntax error near unexpected token `
'ash: /Users/mac/.bashrc: line 18: `highlightExitCode()
bash: highlightExitCode: command not found
root # 

I get the same results with "sudo -s":

 sudo -s
Password:
in .bashrc but claims to be /bin/bash
: No such file or directory
the path is /Users/mac
$PS1 variable is \u$(highlightExitCode) \$ 
$SHELL variable is /bin/bash
the path is: 
  /Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Developer/Tools:/Users/mac/.nexustools
root
end of this script.
: command not found
: command not found
'ash: /Users/mac/.bashrc: line 18: syntax error near unexpected token `
'ash: /Users/mac/.bashrc: line 18: `highlightExitCode()
bash: highlightExitCode: command not found
root # 

Best Answer

For cases like this with a more detailed dot file, to get a root shell, I would use

sudo -s

If that works, you might add one line to your .bashrc if you like a more detailed prompt: https://superuser.com/questions/866269/use-sudo-s-but-show-current-folder-name

I don’t and want the root prompt to be very different so I know I’m root and not my normal user as that saves pain for me if I forget to get out of root as soon as possible.

If you want an excellent tutorial on root shell for macOS - see https://scriptingosx.com/2018/04/demystifying-root-on-macos-part-2-the-sudo-command/