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

  • Home
  • SEARCH
  • 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 8911769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:01:40+00:00 2026-06-15T04:01:40+00:00

I have a global variable RT=\e[m TITLE=${FG}%s${RT} and have two functions function one {

  • 0

I have a global variable

RT="\e[m"
TITLE="${FG}%s${RT}"

and have two functions

function one
{
   local FG="\e[33m"
   printf "$TITLE" "One"
}

function two
{
   local FG="\e[32m"
   printf "$TITLE" "Two"
}

but the color dont change, how to reuse the $TITLE variable

  • 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-15T04:01:41+00:00Added an answer on June 15, 2026 at 4:01 am

    Short answer: you can’t, bash doesn’t have the equivalent of pointers. The variable $TITLE is assigned with the expansion of the rhs of the assignment character, so $TITLE has the value %s\e[m since $FG is not defined at expansion time, and hence expands to the empty string. As a work-around you could instead do:

    rt=$'\e[m'
    title="%s%s$rt"
    
    one() {
        local fg=$'\e[33m'
        printf "$title" "$fg" "One"
    }
    
    two() {
        local fg=$'\e[32m'
        printf "$title" "$fg" "Two"
    }
    

    And using eval is not really a good option, as eval is evil!

    I’ve also modified a few things from your script:

    • Used lower case variable names (as using upper case variable names is considered bad practice in bash),
    • Use $'...' to have the correct colors (instead of the strings "\e[m", …),
    • Used the proper way to define functions in bash (without the keyword function).

    Edit. From your comment, I see you’re really troubled with having to type "$fg" each time. So here’s another possibility: instead of defining a variable $title, define a function title that echos the formating string and use it like so:

    rt=$'\e[m'
    
    title() {
       echo "$fg%s$rt"
    }
    
    one() {
        local fg=$'\e[33m'
        printf "$(title)" "One"
    }
    
    two() {
        local fg=$'\e[32m'
        printf "$(title)" "Two"
    }
    

    Each time you call the function title, it echoes the formating string you need, hence $(title) will expand to that formating string. Each time you call the function title, the string "$fg%s$rt" is expanded, with whatever values the variables $fg and $rt have at this expansion time.

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

Sidebar

Related Questions

In following program . I have one doubt. I have declared one global variable
If i have global variable in A.dll, that depends on global variable in B.dll
I have a global variable averagel that I am trying to manipulate in a
I have a global variable contacts, var contacts = ContactsApp.getContacts(); I have an KeyUphandler
I have a global variable that is an instance of my custom class. How
I have a global pointer variable char* pointer = new char[500]; /* some operations...
If I have a global static variable x like in this code #include <stdio.h>
I have the following global in variable in file_1.pl, in package main: package main;
I have a header file variable.h where i declare all my global variable.Then i
I have a vector declared as a global variable that I need 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.