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 7942687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:03:04+00:00 2026-06-04T00:03:04+00:00

In my Bash script, I have a function to return 0 or 1 (true

  • 0

In my Bash script, I have a function to return 0 or 1 (true or false) for the later main function’s condition.

function1 () {
    if [[ "${1}" =~ "^ ...some regexp... $" ]] ; then
        return 1
    else
        return 0
    fi
}

Then in my main function:

main () {
    for arg in ${@} ; do
        if [ function1 ${arg} ] ; then
            ...
        elif [ ... ] ; then
            ...
        fi
    done
}

However, when I ran this script it always gave me an error message:

[: function1: unary operator expected

How can I fix this?

  • 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-04T00:03:05+00:00Added an answer on June 4, 2026 at 12:03 am

    You are making the common mistake of assuming that [ is part of the if command’s syntax. It is not; the syntax of if is simply

    if command; then
        ... things which should happen if command's result code was 0
    else
        ... things which should happen otherwise
    fi
    

    One of the common commands we use is [ which is an alias for the command test. It is a simple command for comparing strings, numbers, and files. It accepts a fairly narrow combination of arguments, and tends to generate confusing and misleading error messages if you don’t pass it the expected arguments. (Or rather, the error messages are adequate and helpful once you get used to it, but they are easily misunderstood if you’re not used.)

    In your main function, the call to [ appears misplaced.  You probably mean

    if function "$arg"; then
        ...
    elif ... ; then ...
    

    By the way, for good measure, you should also always quote your strings. Use "$1" not $1, and "$arg" instead of $arg.

    The historical reasons for test as a general kitchen sink of stuff the authors didn’t want to make part of the syntax of if is one of the less attractive designs of the original Bourne shell. Bash and zsh offer alternatives which are less unwieldy (like the [[ double brackets in bash, which you use in your function1 definition), and of course, POSIX test is a lot more well-tempered than the original creation from Bell Labs.

    As an additional clarification, your function can be simplified to just

    function1 () {
        ! [[ "$1" =~ "^ ...some regexp... $" ]]
    }
    

    That is, perform the test with [[ and reverse its result code. (The “normal” case would be to return 0 for success, but maybe you are attempting to verify that the string doesn’t match?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash script that has this function in it: function start_vi() {
I have a script that looks like this #!/bin/bash function something() { echo hello
Suppose we have a BASH script running some commands in the background. At some
I have some problems with a PHP script that calls a Bash script.... in
I have an exec function in php file that execs a bash script. It
If I have a Bash script like: #!/bin/bash f() { # echo function name,
I have this script #!/bin/bash function labels2 () { awk ' /[0-9]/{ print substr($3,length($3)-11),
I have a bash script file which starts with a function definition, like this:
I have written the following function in a bash script but it is not
I have been trying to run some java programs from a bash script in

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.