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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:44:49+00:00 2026-06-03T00:44:49+00:00

I am wondering what’s the easiest way to check if a program is executable

  • 0

I am wondering what’s the easiest way to check if a program is executable with bash, without executing it ? It should at least check whether the file has execute rights, and is of the same architecture (for example, not a windows executable or another unsupported architecture, not 64 bits if the system is 32 bits, …) as the current system.

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

    Take a look at the various test operators (this is for the test command itself, but the built-in BASH and TCSH tests are more or less the same).

    You’ll notice that -x FILE says FILE exists and execute (or search) permission is granted.

    BASH, Bourne, Ksh, Zsh Script

    if [[ -x "$file" ]]
    then
        echo "File '$file' is executable"
    else
        echo "File '$file' is not executable or found"
    fi
    

    TCSH or CSH Script:

    if ( -x "$file" ) then
        echo "File '$file' is executable"
    else
        echo "File '$file' is not executable or found"
    endif
    

    To determine the type of file it is, try the file command. You can parse the output to see exactly what type of file it is. Word ‘o Warning: Sometimes file will return more than one line. Here’s what happens on my Mac:

    $ file /bin/ls    
    /bin/ls: Mach-O universal binary with 2 architectures
    /bin/ls (for architecture x86_64):  Mach-O 64-bit executable x86_64
    /bin/ls (for architecture i386):    Mach-O executable i386
    

    The file command returns different output depending upon the OS. However, the word executable will be in executable programs, and usually the architecture will appear too.

    Compare the above to what I get on my Linux box:

    $ file /bin/ls
    /bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped
    

    And a Solaris box:

    $ file /bin/ls
    /bin/ls:        ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped
    

    In all three, you’ll see the word executable and the architecture (x86-64, i386, or SPARC with 32-bit).


    Addendum

    Thank you very much, that seems the way to go. Before I mark this as my answer, can you please guide me as to what kind of script shell check I would have to perform (ie, what kind of parsing) on ‘file’ in order to check whether I can execute a program ? If such a test is too difficult to make on a general basis, I would at least like to check whether it’s a linux executable or osX (Mach-O)

    Off the top of my head, you could do something like this in BASH:

    if [ -x "$file" ] && file "$file" | grep -q "Mach-O"
    then
        echo "This is an executable Mac file"
    elif [ -x "$file" ] && file "$file" | grep -q "GNU/Linux"
    then
        echo "This is an executable Linux File"
    elif [ -x "$file" ] && file "$file" | grep q "shell script"
    then
        echo "This is an executable Shell Script"
    elif [ -x "$file" ]
    then
        echo "This file is merely marked executable, but what type is a mystery"
    else
        echo "This file isn't even marked as being executable"
    fi
    

    Basically, I’m running the test, then if that is successful, I do a grep on the output of the file command. The grep -q means don’t print any output, but use the exit code of grep to see if I found the string. If your system doesn’t take grep -q, you can try grep "regex" > /dev/null 2>&1.

    Again, the output of the file command may vary from system to system, so you’ll have to verify that these will work on your system. Also, I’m checking the executable bit. If a file is a binary executable, but the executable bit isn’t on, I’ll say it’s not executable. This may not be what you want.

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

Sidebar

Related Questions

Wondering whether there is an efficient way to add an item to Java's ArrayList
Wondering if anyone has written or come across a good way to log to
Wondering if there is any way to get the lambda expressions that result from
Wondering if anyone can suggest a good file replication tool that will replicate across
Wondering if there is a good way to generate temporary URLs that expire in
Wondering if anyone has a good solution for this. My app is displaying nothing
Wondering if anyone has had any experience using retina assets with the NativeControls iPhone
Wondering what would be the most efficient way to do the following. Game play.
Wondering if one file is open for writing in and another programe is accessing
Wondering if anyone has played around with GitHub's new support for SVN? Would really

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.