6

I have an alias in my bash_profile that is currently one really long line like this:

alias staging_server='MY_ENV1="http://example.com" MY_ENV2="http://example2.com" MY_ENV3="http://example3.com" MY_ENV4="http://example4.com" start_server -p 1234'

Is there a way to split this up with newlines using a function or alias to make it more legible? Something like this (which doesn't seem to work)?

alias staging_server=' \
  MY_ENV1="http://example.com" \
  MY_ENV2="http://example2.com" \
  MY_ENV3="http://example3.com" \
  MY_ENV4="http://example4.com" \
  start_server -p 1234
'

I'd like to avoid exporting these as I don't want them as the defaults.

4
  • Works for me; how does it not work for you?
    – Jeff Schaller
    Dec 7, 2017 at 17:22
  • 1
    alias foo=' \ (ENTER) E1=1 \ (ENTER) E2=2 \ (ENTER) env | grep ^E.= (ENTER) '
    – Jeff Schaller
    Dec 7, 2017 at 17:23
  • Just use a function
    – Dani_l
    Dec 7, 2017 at 17:23
  • So, it turns out this was all just an issue with some Tmux weirdness. I'm not really sure what was happening, but the alias started working after I killed my session (yes, I was sourceing the whole time, it just wasn't doing anything I guess). Still, I ended up using a function anyway, so I'm glad I asked, thanks everyone! :-)
    – Goro
    Dec 7, 2017 at 19:43

3 Answers 3

13

Note that exporting would be fine (would not affect the rest of the shell session) if you did it in a subshell like:

staging_server() ( # <-- note the ( instead of { here.
  set -o allexport
  MY_ENV1="http://example.com"
  MY_ENV2="http://example2.com"
  MY_ENV3="http://example3.com"
  MY_ENV4="http://example4.com"
  exec start_server -p 1234 "$@"
)

Note that it doesn't imply an additional fork, we're just doing the fork earlier here.

The only case where that would imply an additional fork would be if you used a shell builtin in place start_server there (in which case in some shells like bash (one of those shells where that exec is needed as it's not done implicitly), using exec would not call that shell builtin).

5

The alias actually seems to work for me like that (provided that there's no whitespace after the backslashes). But a function might be nicer and at least makes it easier to use single-quotes:

staging_server() {
    MY_ENV1='http://example.com' \
    MY_ENV2="..." \
    start_server -p 1234 "$@"
}
3
  • 1
    Why the backslashes in the function body?
    – Dani_l
    Dec 7, 2017 at 17:28
  • 2
    @Dani_l, because they didn't want to export the variables. that's the same difference as between FOO=blah somecmd and FOO=blah; somecmd.
    – ilkkachu
    Dec 7, 2017 at 17:33
  • Put the body of the function in (...) rather than in {...;} to make the variables local (regarding @Dani_l's comment).
    – Kusalananda
    Jan 29, 2018 at 10:37
3

If you need environment variables, you can use the env command:

alias staging_server='env \
  MY_ENV1="http://example.com" \
  MY_ENV2="http://example2.com" \
  MY_ENV3="http://example3.com" \
  MY_ENV4="http://example4.com" \
  start_server -p 1234
'
1
  • Why not just use alias comm='MY_ENV="abc" \comm'? Jun 26, 2018 at 2:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .