Skip to main content

All Questions

Tagged with
1 vote
1 answer
290 views

How to call many shell functions from sudo?

I have a huge alias file sourced by ~/.bashrc or ~/.zshrc which contains both simple aliases and more functions for more complex stuff: # example aliases alias "sudo=sudo " alias "...
Polizi8's user avatar
  • 285
5 votes
1 answer
2k views

Why doesn't 'sudo -E' preserve the function environment variables exported by export -f?

In script.sh: #!/bin/bash func(){ echo "I am here a func." } export -f func export variable="I am here a variable" sudo -E bash -c "func ; echo $variable" And the output came: bash: func: ...
Anon's user avatar
  • 415
-1 votes
1 answer
1k views

Run .bashrc function as sudo [duplicate]

I added this to ~/.bashrc to provide colorized tail: tailc () { tail $@ | ccze } And I can run it as so: tailc -f -n 10 foo/log.txt But I can't use it on a privileged file: sudo tailc -f -n 10 /...
lonix's user avatar
  • 1,945
1 vote
2 answers
7k views

sudo without sudo, implying sudo in script

I made some scripts containing some functions which by design needs sudo permission. I have added those path in .bashrc for Linux and .bash_profile for MacOS so that it can be called from anywhere. ...
Rakib Fiha's user avatar
1 vote
2 answers
1k views

How can I change user inside function

I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo works as expected outside function and didn't work inside bash function. I run script as ssh ...
tsh's user avatar
  • 127
49 votes
13 answers
40k views

Executing a Bash Script Function with Sudo

I have a script that does a number of different things, most of which do not require any special privileges. However, one specific section, which I have contained within a function, needs root ...
BryKKan's user avatar
  • 2,267
0 votes
2 answers
349 views

Bash function for piping data into protected file [duplicate]

I wanted to create a .bashrc function that would simplify passing data to a write-protected file. function pipe { sudo bash -c "$1" } Unfortunately the command pipe echo something > /etc/...
adam767667's user avatar
11 votes
3 answers
18k views

Forward function and variables into sudo su - <user> <<EOF

I have declared functions and variables in bash/ksh and I need to forward them into sudo su - {user} << EOF: #!/bin/bash log_f() { echo "LOG line: $@" } extVAR="yourName" sudo su - <user&...
waldauf's user avatar
  • 375
4 votes
3 answers
4k views

Run nvm (bash function) via sudo

I want to write a init script that should basically run nvm use v0.11.12 && forever start /srv/index.js as the user webconfig. nvm is a shell function that is declared in ~webconfig/.nvm/nvm....
Yogu's user avatar
  • 147
6 votes
1 answer
2k views

How to enable `sudo` with custom functions?

Recently I learned you can enable sudo for custom aliases as follows: alias sudo='sudo ' # note: the space is required! The reason this works is the following: If the last character of the alias ...
Mathias Bynens's user avatar
3 votes
5 answers
6k views

How can I use sudo within a function?

I have written a function which acts in a similar way to tee but also pre-pends a datestamp. everything works fine except when i want to output to a file which is only root writable (in my case a ...
mulllhausen's user avatar
  • 2,721