Yes, you could write a bash script which uses xrandr to set up your screen preferences and then have that set up to run when you log-in.
But the exact xrandr command would depend on what you want to do.
When the external monitor is plugged in, do you want to turn the laptop screen off and use ONLY the external monitor? Or do you want to use it as an additional monitor?
Whatever you want to do, you should be able to do it using xrandr.
Here's a brief article:
https://www.maketecheasier.com/how-to-setup-dual-monitors-with-xrandr/
And here is the Arch wiki page on xrandr:
https://wiki.archlinux.org/index.php/Xrandr
Even if you don't use Arch linux - the arch wiki contains a lot of detailed information about thousands of pieces of software and is an excellent resource! I haven't used Arch in years, but I still find myself referring to their wiki from time to time, when I get stuck on something!
Using xrandr, you can enable and disable different screens. With a multi-monitor setup, you can specify which screen is primary. You can also independently set up each screens resolution, rotation (if you have your wide-screen monitor rotated in portrait mode rather than landscape) and its position (relative to the primary screen) - e.g. left-of, right-of, above, below. You can also set up many other more advanced options like transformation matrices for each display and other advanced, or obscure features that a display may support. There is also an xrandr command that will show the available capabilities/options for all connected screens/outputs.
I use two scripts on my linux laptop at home. One called "hdmiscript" and another called "vgascript".
If I connect my laptop to the external monitor in my bedroom via VGA, I use vgascript to set it up as a secondary monitor to the right of my laptop screen.
If I connect my laptop to a TV via HDMI, I run my hdmiscript and it will set up the TV as a secondary monitor to the right of my laptop screen.
Why always to the right? - IDK - I just like the extra screen being to the right!
Both of my scripts use xrandr to connect my laptop to specific screens and sets the appropriate resolution for them. But I don't use external monitors/TVs very often, so I manually run my scripts when I need them. But there would be nothing stopping you from setting up a script that will run on startup, or when you log in.