Is there any way to execute small bash snippets in the shell?

If you don't want to edit a file again and again and test it by executing it again and again (in fact by jumping forth and back between two programs: editor and shell), you might want to execute small code snippets (maybe as strings) in the shell directly.

For example testing a regular expression or any part of an if-condition: "hi =~ "h". OK, the result of this might be obvious, but there are more complicated cases for one, two or three liner.

So, none of these ways, because, AFAIK bash -x executes files and eval executes commands only.

closed as off-topic by jasonwryan, Michael Homer, taliezin, terdon Jul 1 '15 at 8:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – jasonwryan, Michael Homer, taliezin, terdon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    If you're running Bash you can literally write the code at the prompt; can you be clearer about what you've tried and why it isn't adequate for you? – Michael Homer Jul 1 '15 at 6:51
  • mhm, how can I achive that? I mean, apart from echo "hi", e.g. smth like if [ "hi" == "hi" ] then echo "this is true" fi of course does not work (like the equivalent in JS would work in a browser console. – BairDev Jul 1 '15 at 7:00
  • You're missing semicolons before then & fi. – Michael Homer Jul 1 '15 at 7:01

You can run it in the shell, for example like:

$ for i in $(find /etc -type f); do wc -l $i; done

This runs through /etc and gives out the line count of each file.

Added according your comment:

$ if [ "hi" == "hi" ]; then echo "this is true"; fi

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