2

I have the idea that I would like to add an environment variable ("waitTillReady") to my ~/.bashrc file, such that instead of doing this:

while [ "$(pidof -s make)" -o "$(pidof -s gcc)" -o "$(pidof -s ld)" ]; do echo "Still compiling..." && sleep 1; done && printf "\n%.0s" {1..20} && date && echo "THINGS ARE READY FOR YOU NOW..."

I want to be able to do this: $waitTillReady && echo "THINGS ARE READY FOR YOU NOW..."

I tried to edit my ~/.bashrc file like with different combinations of the following:

...
export waitTillReady="while [ "$(pidof -s make)" -o "$(pidof -s gcc)" -o "$(pidof -s ld)" ]; do echo "Still compiling..." && sleep 1; done && printf "\n%.0s" {1..20} && date"
...

E.g. I tried to enclose everything insider a single quote ' and I tried to escape the \" in the middle... Could anyone please help and tell what I should write in the ~/.bashrc file, after the "export waitTillReady=" line?

I would be grateful to hear any suggestions/ideas (and maybe an explanation of what I'm doing wrong), thanks!

2
  • you want an alias or a function, not export... – pLumo Jul 17 '19 at 12:03
  • Oh, thanks - I see... So it isn't possible to make as an EXPORT (not that I absolutely want that, just asking of curiosity) ? – Okay Dokey Jul 17 '19 at 12:10
3

If the syntax was correct, export would run your code only once, when your terminal starts and sets the result as an environment variable, which you can call using $waitTillReady.

You need an alias or maybe easier a shell function.

Put this in your .bashrc:

waitTillReady() {
    while pgrep '^(make|gcc|ld)$' &>/dev/null; do
        echo "Still compiling..." && sleep 1
    done && printf "\n%.0s" {1..20} && date
}

Then after next start of your terminal you can use it like it was a command:

waitTillReady; do_something
3
  • Interesting, thanks. Just to be clear - there's no way to make something like I originally started out with work, right (with "EXPORT ... [something]")? Because of some shell expansion stuff or...? – Okay Dokey Jul 17 '19 at 12:37
  • 1
    you would need command substitution like this: export variableName=$(command), but like I said, this will run only once when the terminal starts. – pLumo Jul 17 '19 at 12:39
  • ok: "... will run only once when the terminal starts" - that's not good enough :-) Thanks, I've marked as answered and am very happy now, it works for me with both the function - and the alias... It'll make things a bit easier, thanks. – Okay Dokey Jul 17 '19 at 12:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.