我如何使用sudo在bash子外壳中以另一个用户身份执行一系列命令? [英] How can I execute a series of commands in a bash subshell as another user using sudo?

查看:75
本文介绍了我如何使用sudo在bash子外壳中以另一个用户身份执行一系列命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个bash脚本,该脚本需要sudo多个命令.我可以这样做:

I'm writing a bash script that needs to sudo multiple commands. I can do this:

( whoami ; whoami )

但是我不能这样做:

sudo ( whoami ; whoami )

我该如何解决?

推荐答案

sudo内部运行shell: sudo bash -c 'whoami; whoami'

Run a shell inside sudo: sudo bash -c 'whoami; whoami'

您可以在单引号内使用除'本身以外的任何字符.如果您确实想在该命令中使用单引号,请使用'\''(从技术上讲,是:结束单引号文字,文字'字符,以单引号文字开头;但是实际上,这是一种注入单引号的方式用单引号引起来的单引号字符串).

You can use any character except ' itself inside the single quotes. If you really want to have a single quote in that command, use '\'' (which technically is: end single-quote literal, literal ' character, start single-quoted literal; but effectively this is a way to inject a single quote in a single-quoted literal string).

这篇关于我如何使用sudo在bash子外壳中以另一个用户身份执行一系列命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆