How Can I Pass an Alias to Sudo?

I have tried putting my aliases in ~/.bash_profile, ~/.bashrc, /etc/profile, and /etc/bashrc.

I am still unable to execute the following:

alias zf2="php public/index.php"

and then execute:

sudo zf2 orm:info

The issue seems to be that I am unable to specify an alias as a command using visudo-- which causes a syntax error.

So I am unable to call:

sudo zf2 orm:info

However, I was able to create a script at /usr/share/scripts/zf2 which contains:

#!/bin/bash

alias zf2="php public/index.php"
zf2 $1

and add this script as the command in visudo. When this script is in the end user's PATH I am able to execute

zf2 orm:info

I have different aliases like zf2 that I need to expose to the end user. I would prefer to maintain alias instead of a collection of scripts.

5

1 Answer

Ironically, the solution is to call sudo from an alias.

alias sd="sudo "

Note: While not recommended, you could name the alias sudo: alias sudo="sudo "

Bash Reference Manual (Aliases)

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest