Skip to content

Latest commit

 

History

History
31 lines (19 loc) · 883 Bytes

SC2232.md

File metadata and controls

31 lines (19 loc) · 883 Bytes

Pattern: Invalid argument to sudo

Issue: -

Description

Due to the Unix process model, sudo can only change the privileges of a new, external process. It cannot grant privileges to a currently running process.

This means that shell builtins - commands that are interpreted by the current shell rather than through program invocation - cannot be run with sudo. This includes cd, source, read, and others.

Instead you can run a shell with sudo, and have that shell run the builtins you want:

sudo sh -c 'cd /root && pwd'  # This shows /root
pwd                           # This shows the original directory

Example of incorrect code:

sudo cd /root
pwd

Example of correct code:

sudo sh -c 'cd /root && pwd'

Further Reading