Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:06:00+00:00 2026-06-10T20:06:00+00:00

As bash doesn’t have first class functions, I’m simulating anonymous functions by passing a

  • 0

As bash doesn’t have first class functions, I’m simulating anonymous functions by passing a string to a function, which then gets evaluated by eval.

it() {
    echo "$1" # prints the description of the spec
    beforeEach # a setup function
    eval "$2"

    if (($? == 0)); then
        # do something
    fi

    afterEach  # a cleanup function
}

it "should echo something" '{
  echo "something"
}'

This allows to write very concise tests (it defines a specification). Now I wonder if this is a valid use of eval.

EDIT

I am aware that the opening { and closing } are not need in the anonymous function string, it’s just that that way it resembles for something like Jasmine.

EDIT

The pseudo anonymous function is actually a test, which means at the end of the test, there is something like

[[ var == 'foo' ]]

or

((i > 10))

i.e. some sort of test (or assert, in XUnit terms). It never needs to return anything than the return code which then gets evaluated, and if the return code is 0 (success), the description is printed in green, otherwise red (the test failed).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:06:01+00:00Added an answer on June 10, 2026 at 8:06 pm

    If it gets the job done and you (and ideally others) understand how it works, absolutely.

    I’ve been writing similar code for test case automation more than once or twice, but I admit it can get pretty hairy, especially if you start simple and then allow it to grow organically.

    For what it’s worth, I would like to suggest a stylistic improvement;

    if eval "$2"; then
      # something
    

    or if the “something” is a simple command, plain old

    eval "$2" && # something
    

    Having said that, it might be better if you can avoid the eval altogether, but this depends on your test cases.

    $2 && # something
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried exporting the function and then executing it with bash, but that doesn't
I have a bash script that uses growlnotify to send notifications. However, growlnotify doesn't
I have a bash file, which I’m trying to run in Cygwin on a
I have a bash function that produces some output: function scan { echo output
If I place this on the top the Bash script, Control+C doesn't work. exit-function()
Why doesn't this print all the passed arguments, in bash? function abc() { echo
In a sort of try/catch form I want to execute a bash that doesn't
why doesn't this work on a bash command line: cat `echo 'filename with spaces'`
I'd like to generate dummy files in bash. The content doesn't matter, if it
Here's what I'm trying to achieve: open a shell (korn or bash, doesn't matter),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.