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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:35:42+00:00 2026-06-12T12:35:42+00:00

In my script I’m trying to take a string, then output the extension of

  • 0

In my script I’m trying to take a string, then output the extension of a string if it has one. So essentially I take the basename of a file, then output anything that comes after a period.

What’s the syntax to do something like this?

For example

dotcutter.sh
file=testfile.jpg

the script should output .jpg

EDIT:

I’ve solved my problem now with:

file=$(basename "$1" )
stub=${file%.*}
echo ${file#"$stub"}

Which reduces my argument to a basename, thank you all.

  • 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-12T12:35:43+00:00Added an answer on June 12, 2026 at 12:35 pm
    $ file=testfile.jpg
    $ echo ${file##*.}
    jpg
    $ file=testfile.ext1.ext2
    $ echo ${file##*.}
    ext2
    $ file=noextension
    $ echo ${file##*.}
    noextension
    

    Notice that it doesn’t work if the file has no extension. If that’s important then try this two-step solution:

    $ file=testfile.ext1.ext2
    $ stub=${file%.*}
    $ echo ${file#"$stub"}
    .ext2
    

    Or this regex-based one, which will only call echo if there’s actually an extension. (&& is shorthand for “if then”.)

    $ regex='(\.[^.]*)$'
    $ file=testfile.ext1.ext2
    $ [[ $file =~ $regex ]] && echo ${BASH_REMATCH[1]}
    .ext2
    $ file=noextension
    $ [[ $file =~ $regex ]] && echo ${BASH_REMATCH[1]}
    

    See also:

    • basename(1)
    • dirname(1)
    • Bash FAQ: How can I use parameter expansion? How can I get substrings? How can I get a file without its extension, or get just a file’s extension?]
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my script is using utf8_general_ci and im trying to transfer to another script that
Script adds id for block, gives: <div id=some>Text</div> After that page should jump for
I have a small JavaScript validation script that validates inputs based on Regex. I
Script test.sh: #!/bin/bash if [[ $# -eq 0 ]]; then echo no arg else
JRuby script.rb throws many Java errors. I tried outputting to a text file, but
Write Script to read a positive integer number then it computes the following sequence:
script/console output: >> User.find(:first, :select => '`email`, `pass`, `login`, `id`') => #<User pass: e10adc3949ba59abbe56e057f20f883e,
PHP script $count = 0; foreach ($_FILES['filesToUpload'] as $file) { //upload process echo $file[$count]['tmp_name'].',';
Script (originally copied from here ) takes a fixed-width text file as input, rearranges
Script: var degress = GiveDegrees(); $(function () { //alert(GiveDegrees()); //output: 89,91,87,90,91 }); function GiveDegrees()

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.