0

I'm interest in some thing : every time I echo $RANDOM , the show value difference . I guess the RANDOM is special (When I read it , it may call a function , set a variable flag and return the RANDOM number . ) . I want to create a variable like this , how I can do it ? Every answer will be helpful .

5
  • 1
    Yes, it's special. Such variable can only be created by modifying Bash's source code. Commented Apr 6, 2015 at 0:25
  • Try $func(){do something} then call $func Commented Apr 6, 2015 at 0:28
  • I can't do it with $func, but can func . My bash version is 4.2.53(1). Commented Apr 6, 2015 at 0:43
  • 3
    @LinuxLiker : you are correct, the $ is not a valid character for a function name. just use func. If you need that value in a variable, the put it there with myVar=$(func). Good luck. Commented Apr 6, 2015 at 1:02
  • What is your exact requirement? Maybe you can have any general variable & its value can be set via PROMPT_COMMAND (Applies for interactive shells only) Commented Apr 6, 2015 at 4:10

3 Answers 3

1

The special behavior of $RANDOM is a built-in feature of bash. There is no mechanism for defining your own special variables.

You can write a function that prints a different value each time it's called, and then invoke it as $(func). For example:

now() {
    date +%s
}

echo $(now)

Or you can set $PROMPT_COMMAND to a command that updates a specified variable. It runs just before printing each prompt.

i=0
PROMPT_COMMAND='((i++))'

This doesn't work in a script (since no prompt is printed), and it imposes an overhead whether you refer to the variable or not.

Sign up to request clarification or add additional context in comments.

Comments

1

If you are BASH scripting there is a $RANDOM variable already internal to BASH. This post explains the random variable $RANDOM:

http://tldp.org/LDP/abs/html/randomvar.html

It generates a number from 0 - 32767.

If you want to do different things then something like this:

case $RANDOM in
[1-10000])
  Message="All is quiet."
  ;;
[10001-20000])
  Message="Start thinking about cleaning out some stuff.  There's a partition that is $space % full."
  ;;
[20001-32627])
  Message="Better hurry with that new disk...  One partition is $space % full."
  ;;
esac

Comments

1

I stumbled on this question a while ago and wasn't satisfied by the accepted answer: He wanted to create a variable just like $RANDOM (a variable with a dynamic value), thus I've wondered if we can do it without modifying bash itself.

Variables like $RANDOM are defined internally by bash using the dynamic_value field of the struct variable. If we don't want to patch bash to add our custom "dynamic values" variables, we still have few other alternatives.

An obscure feature of bash is loadable builtins (shell builtins loaded at runtime), providing a convenient way to dynamically load new symbols via the enable function:

$ enable --help|grep '\-f'
enable: enable [-a] [-dnps] [-f filename] [name ...]
      -f        Load builtin NAME from shared object FILENAME
      -d        Remove a builtin loaded with -f

We now have to write a loadable builtin providing the functions (written in C) that we want use as dynamic_value for our variables, then setting the dynamic_value field of our variables with a pointer to the chosen functions.

The production-ready way of doing this is using an another loadable builtin crafted on purpose to do the heavy-lifting, but one may abuse gdb if the ptrace call is available to do the same.

I've made a little demo using gdb, answering "How to create a bash variable like $RANDOM?":

$ ./bashful RANDOM dynamic head -c 8 /dev/urandom > /dev/null
$ echo $RANDOM
L-{Sgf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.