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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:41:42+00:00 2026-05-23T11:41:42+00:00

My bash scripts has a variable $c which is called within a sed -line,

  • 0

My bash scripts has a variable $c which is called within a sed-line, directly followed by another parameter – and bash (actually fairly logical) seems to think that the other parameter belong to the variable name, rendering it useless/empty.

(...)
c=$(printf "%02d" $(echo "$i+1"|bc))
sed -n "$cq;d" /var/www/playlisten.txt|cut -c 4-
(...)

The first line sets a temp variable, then it is called as a sed argument. I need to show bash that $cends after the c and that the variable is not named $cq (which is empty, of course)…

Any ideas would be highly appreciated, as always.

Thanks,
Christian.

PS. What I’m trying to accomplish 🙂 with this is having a for-loop that steps thru 00..50, within the loop the number itself is needed but also the number +1. Just in case anyone want’s to know.

  • 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-23T11:41:43+00:00Added an answer on May 23, 2026 at 11:41 am

    You need to use ${c}q to prevent the greedy treatment (bash trying to use as many valid characters as possible):

    pax$ export cq=111
    
    pax$ export c=222
    
    pax$ echo $cq
    111
    
    pax$ echo ${c}q
    222q
    

    I should also mention that, if performance is important to you, you want to try and minimise how many external processes (like bc) you run to do your task. Forking and exec’ing are not cost-free actions and you’ll run much faster if you get bash to do most of the work itself for short lived tasks.

    By short-lived I mean things like adding one to a variable. Obviously if you want to do a big job like sed an entire file, you’re better off doing that in a dedicated external tool but bash provides quite a bit of power to replace expensive operations like i=$(expr $i + 1) or i=$(echo "$i+1"|bc).

    A bash loop can be done thus with the increment and other calculations being handled without external processes:

    #!/bin/bash
    for ((count = 0; count < 10; count++)) ; do
        ((next = count + 1))
        echo "count is ${count}, sed string is '${next}q;d'."
    done
    

    This outputs:

    count is 0, sed string is '1q;d'.
    count is 1, sed string is '2q;d'.
    count is 2, sed string is '3q;d'.
    count is 3, sed string is '4q;d'.
    count is 4, sed string is '5q;d'.
    count is 5, sed string is '6q;d'.
    count is 6, sed string is '7q;d'.
    count is 7, sed string is '8q;d'.
    count is 8, sed string is '9q;d'.
    count is 9, sed string is '10q;d'.
    

    You could also incorporate next into the for loop as well:

    #!/bin/bash
    for ((count = 0, next = 1; count < 10; count++, next++)) ; do
        echo "count is ${count}, sed string is '${next}q;d'."
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash shell script which has the line: g=$(/bin/printf ${i}) when ${i}
I am working on a BASH script which has a global variable. The value
I have a few bash and perl scripts which has been asked to be
We have a system that has some Bash scripts running besides Java code. Since
I have some ksh scripts which I'd like to convert to run with bash
I have a bash shell script which calls some python scripts. I am running
While writing BASH scripts, I generally use the which command of a Linux machine
I'm trying to run a bash script that has: ssh -l <username> <compname> 'sudo
I have certain critical bash scripts that are invoked by code I don't control,
I'm writing a few little bash scripts under Ubuntu linux. I want to be

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.