 Recently, I was reading an article in the Linux Journal and there was a example of how to scramble words in a shell script. And let's go ahead and do it their way and then look at alternatives. So first of all, we need a word to scramble, word or sentence to scramble. So I'll say echo hello world from Linux, sure. So of course we echo that out, just echoes that out. Let's pipe that into said and then what we're going to do with said here is we're going to replace things. We're going to say dot and then we're going to say end backslash n. So what this is saying is take every character and basically put each character on its own new line. We're adding a new line after each character so when we do that, we have our sentence now printed out one character per line. Next we're going to pipe that into the shuff command. What shuff does is it shuffles lines of text. So now we can do that and now our lines of text are all shuffled up. And now we just need to put it back together on one line. So what we would do is take that same line and we're going to pipe that into tr which will take one character and replace it with another or in this case we're going to use dash d which means find this character and delete it and we're going to do backslash n which means new line. So basically we're splitting each character onto its own line, shuffling all the lines and then we're removing all the new line characters and that gives us the word all shuffled up. Of course our new shell line ends up at the end of the line because we've removed all new lines but here is our sentence all shuffled up. And that's great. A few things though. One shuff is not always available on all systems said in tr are basic tools but shuff for shuffled is not always there but tr instead they're fairly common they're in busy box even on a lightweight system they're there. But can we do this in pure bash? See we're now piping into external commands, three external commands outside of bash. Can we do it purely in bash and can we do it where we create a function so we can easily call it. Let's go ahead and do that. And let's go ahead and just create a script for it I guess. I was going to write it out in the shell but now I'm thinking we'll just call it. Well no let's let's do it right in the shell here and then we can add it to a script later on. We'll call it function scramble so we're going to call the function scramble. Then we're going to go local by way I hit shift enter to create a new line without running that command. We'll create a variable, a local variable in this function called s and we're going to say dollar sign asterisk i. So dollar sign asterisk is saying basically take whatever you're passing this to this script and put it in s and i is just creating another variable. Now we're going to say while and in here we're going to say dollar sign brackets pound s sorry yeah brackets parentheses parentheses semicolon and then we're going to say do then we'll say inside these braces and I'll explain all this momentarily and I'm starting to regret typing this all out in the shell not in a script file s there we go aren't you glad you came here just to watch me type again I'll explain everything in a minute s colon i colon one now we'll say s equals dollar sign s colon colon i bracket or curly brace whatever you don't call it s colon i plus one close that done and now close that off and we have created a function so now we should be able to say scramble hello from Linux or hello world from Linux we want to do the same thing from Linux and actually let's say bash aside we're using there we go we got our words all scrambled now that might seem a little more complicated technically if you want to go about it's more efficient because you're not starting new processes you're just using built-in bash functions and again that makes a little bit simpler you don't have to worry about things like shuff not being available but once you create the function you can call that function whenever you'd like especially if you use it inside a script but what are we doing here so again we're calling this function and then passing it some arguments the dollar sign asterisk says you know put into this local variable s everything that's passed this function so everything you type after that then we're going to go through each character of that variable s that's what this means while we're going through each character we are going to find the a random character in there actually said that wrong there's not each character this is the number of characters it's the length so it's going to calculate the length of characters and we're going to go through each one and we're going to use random to get a random character in that range so we're saying let's let's just say there's there's 20 characters and how many there are we're saying find a random character basically from 0 to 20 right here and we're putting that in I now we're going to say echo with no new line at the end s whatever character we just randomly grabbed just one character so we're going to find that variable go to a character print that one character we're echoing that out and then we're going to take s and we're going to basically what this is saying is find that character again and remove that character from s so then we loop again so before it was 20 characters now it's 19 so it's finding a random number between 0 and 19 and then it's going to go through that again and find a random character in there print it out without a new line character and then it's going to remove that character and replace the variable s I hope I explain that well I know I kind of messed up here a little bit but again this is saying number of characters it's the length of whatever string that we place in there printing out a random character and then removing that random character and looping over again until we run out of characters the reason this works with the while loop is it's as long as this you know isn't zero so once you get to zero it breaks out of the loop so and again we can type whatever we want in here we can type in this is a test of things that happen in the shell I don't know it's big stuff up and there we go we got our characters all scrambled up now this is not scrambling individual words scrambling the entire line but that's it that's it impure bash so if you want to do it quick and simple one liner here you can do that using external tools running multiple processes or just create a function in your script just like this and then you can call that function whenever you want so I hope you enjoyed that hope that made sense you have two options there in reality you know if I'm just doing this from memory I'm going to do it like this but this function here I can save this in my templates and Vim and then I can import that function I can use Vim to to import that template or I have I done the tutorial on importing functions from into scripts I don't know if I have I need to do that if I haven't anyway you can have a library with your functions in it that you call or just add this function with as a template in whatever editor you use for your scripts so films by Chris dot com that's Chris the K there's a link in the description be sure to check that out as well as my patreon page patreon.com forward slash malix 1000 we all know what patreon is I really appreciate a support if you support me on patreon you get early access downloads of the videos so you don't have to wait for them and it really helps me out if you can't use patreon for some reason you can use PayPal go to filmsbycris.com and go to the support section there there's links to both my PayPal and patreon if you can support me there if you can't support me financially think about supporting me through like share of subscribes and comments and all that fun stuff look forward to seeing you in the next video have a great day