How to make oneliners work with sudo
Ever do a single line shell script such as:
and that is basically, writing “stuff for end of file” to the end of the file named: target_file.txt.
This is really handy because I don’t need to start up an editor to go in an manually write it out. For one line items, it’s perfect.
The Real Problem
Well, this technique works well when working with normal access files,
special access with sudo
is not required.
With system files such as /etc/hosts
, which controls local mapping
of addresses to names (also known as local DNS
entries),
this is what happens:
Let’s try with sudo
, which I have access:
So, what gives?! I have access, do I really have to sudo vim
here??? Ugh, why did I even bother learning these shell tricks?!
A Solution
There’s another utility in the system called sh
, which is an alias
to the command interpreter. On my system, sh
points to dash
There’s another body of knowledge I have to check out. ($ man dash
has a slew of information!)
Anyways, to use sh
to help solve my oneliner problem, use sh -c
,
which allows execution of arbitrary programs together. Join this with
sudo
and we get:
And voilà, my oneline can work with sudo
!
One thing to note: for sh -c
to accept the oneliner, wrap the
oneliner program with matching quotes, single or double. The starting
quote style must also be the ending style. So if '
is first, the
oneliner must use "
, and vice versa.
The above example with quotes swapped:
Conclusion
How to solve oneliner problems when using sudo
? Add another command:
sh -c
to solve it!
Whenever I want to do quick oneliners on system files that require
sudo
access, I’ll wrap the command up with sudo -c
, after I tried
the same command on non-system files, of course!