Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $RANDOM #5223

Closed
ksaj opened this issue Oct 22, 2023 · 3 comments
Closed

Add $RANDOM #5223

ksaj opened this issue Oct 22, 2023 · 3 comments

Comments

@ksaj
Copy link

ksaj commented Oct 22, 2023

Hello! I have an idea!

Under bash, zsh, and probably others, if you type "echo $RANDOM" you get a random number. It is a different random number each time it is echoed.

I thought this should be a thing in Xonsh because this post from It's FOSS on Mastodon treats it as a given that "echo $RANDOM" on the (unnamed) CLI will have this effect, but this is not the case for Xonsh.

https://infosec.exchange/deck/@itsfoss@mastodon.social/111277379265186528

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

@AstraLuma
Copy link
Member

Xonsh is not a bourne-ish shell. You want @(random.random()).

@AstraLuma AstraLuma transferred this issue from xonsh/xontrib-template Oct 28, 2023
@anki-code
Copy link
Member

anki-code commented Oct 28, 2023

Hello @ksaj!
In xonsh there are many ways to achieve this.

  1. Using alias:
aliases['rnd'] = lambda: __import__('random').randint(0,100) # one liner implementation
echo $(rnd)
# 23
echo $(rnd)
# 43
  1. Using function:
RND = lambda: __import__('random').randint(0,100)
echo @(RND())
# 43
echo @(RND())
# 53

See xonsh-cheetsheet for more ways.

@anki-code anki-code changed the title Idea: $RANDOM Add $RANDOM Oct 28, 2023
@HLSSLenda
Copy link

@ksaj You can use a @events.on_pre_prompt on your .xonshrc to emulate $RANDOM.

@events.on_pre_prompt
def _():
    import random
    return random.randint(0, 32767)

Now you can use $RANDOM like you would do in bash.

16:48:50 47% jorge@localhost ~ @ $RANDOM
20034
16:48:51 47% jorge@localhost ~ @

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants