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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:16:27+00:00 2026-05-22T02:16:27+00:00

A contrived example… given FOO=/foo/bar/baz this works (in bash) BAR=$(basename $FOO) # result is

  • 0

A contrived example… given

FOO="/foo/bar/baz"

this works (in bash)

BAR=$(basename $FOO) # result is BAR="baz"
BAZ=${BAR:0:1}       # result is BAZ="b"

this doesn’t

BAZ=${$(basename $FOO):0:1} # result is bad substitution

My question is which rule causes this [subshell substitution] to evaluate incorrectly? And what is the correct way, if any, to do this in 1 hop?

  • 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-05-22T02:16:27+00:00Added an answer on May 22, 2026 at 2:16 am

    First off, note that when you say this:

    BAR=$(basename $FOO) # result is BAR="baz"
    BAZ=${BAR:0:1}       # result is BAZ="b"
    

    the first bit in the construct for BAZ is BAR and not the value that you want to take the first character of. So even if bash allowed variable names to contain arbitrary characters your result in the second expression wouldn’t be what you want.

    However, as to the rule that’s preventing this, allow me to quote from the bash man page:

    DEFINITIONS
           The following definitions are used throughout the rest  of  this  docu‐
           ment.
           blank  A space or tab.
           word   A  sequence  of  characters  considered  as a single unit by the
                  shell.  Also known as a token.
           name   A word consisting only of  alphanumeric  characters  and  under‐
                  scores,  and beginning with an alphabetic character or an under‐
                  score.  Also referred to as an identifier.
    

    Then a bit later:

    PARAMETERS
           A parameter is an entity that stores values.  It can be a name, a  num‐
           ber, or one of the special characters listed below under Special Param‐
           eters.  A variable is a parameter denoted by a name.  A variable has  a
           value  and  zero or more attributes.  Attributes are assigned using the
           declare builtin command (see declare below in SHELL BUILTIN COMMANDS).
    

    And later when it defines the syntax you’re asking about:

       ${parameter:offset:length}
              Substring Expansion.  Expands to  up  to  length  characters  of
              parameter  starting  at  the  character specified by offset.
    

    So the rules as articulated in the manpage say that the ${foo:x:y} construct must have a parameter as the first part, and that a parameter can only be a name, a number, or one of the few special parameter characters. $(basename $FOO) is not one of the allowed possibilities for a parameter.

    As for a way to do this in one assignment, use a pipe to other commands as mentioned in other responses.

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

Sidebar

Related Questions

$class = new Class; $foo = json_decode($_POST['array']); In this highly contrived example, I have
This is a contrived example, but lets say I have declared objects: CustomObj fooObj;
This may seem like a somewhat contrived example, but I'm left scratching my head.
Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std;
Contrived example to illustrate: (def nest1 {:a {:b {:c foo}}}) (def nest2 {:d {:e
This is a contrived example of what I want to do, but minimally expresses
This is a very contrived example as it's not easy to explain the context
Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B
In this very contrived example, I have an array with 3 elements that I'm
In this contrived example, I have a static initialization function that is run at

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.